Home Strings [Hackerrank] – Camel Case

[Hackerrank] – Camel Case

by nikoo28
0 comment

Question: Given a camelCase string, find the number of words in the string.

Output: 5

Camel case is a very popular style of naming variables and methods in most of the programming languages. Not only it helps to make the code readable but also ensures that the variable names are unique and easy to remember.
A camel case follows the following rules:-
1. The first word will start with a lower case character
2. The first letter of each subsequent word should be upper case.

To name a variable for InputStreamReader we can have a camelCase notation as inputStreamReader.

In the given question, we are provided with a input string and we have to find the total number of words. To solve this question
– scan the entire string
– for each uppercase letter, add 1 to the total
– finally add 1 to the entire total for the first word and print the result.

public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); int terms = 1; for (int i = 0; i < s.length(); i++) { String letter = Character.toString(s.charAt(i)); if (letter == letter.toUpperCase()) { terms++; } } System.out.println(terms); } }

A working implementation of the code can be found here.

0 comment

You may also like

Enclose codes in [code lang="JAVA"] [/code] tags

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More