CamelCase – HackerRank Solution

In this post, we will solve CamelCase HackerRank Solution. This problem (CamelCase) is a part of HackerRank Problem Solving series.

Task

There is a sequence of words in CamelCase as a string of letters, s, having the following properties:

  • It is a concatenation of one or more words consisting of English letters.
  • All letters in the first word are lowercase.
  • For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, determine the number of words in s.

Example

s = oneTwoThree

There are 3 words in the string: ‘one’, ‘Two’, ‘Three’.

Function Description

Complete the camelcase function in the editor below.

camelcase has the following parameter(s):

  • string s: the string to analyze

Returns

  • int: the number of words in s

Input Format

A single line containing string s.

Constraints

  • 1 <= length of s <= 105

Sample Input

saveChangesInTheEditor

Sample Output

5

Explanation

String s contains five words:

  1. save
  2. Changes
  3. In
  4. The
  5. Editor

Solution – CamelCase -HackerRank Solution

C++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    string s;
    cin >> s;
    int ans=1;
    for(int i=0; i<s.size(); i++) {
        if(s[i]>='A' && s[i]<='Z') ans++;
        
    }
    printf("%d\n",ans);
    return 0;
}

Python

import sys

def camelcase(s):
    res = 1
    for let in s:
        if let.isupper():
            res += 1
    
    if not s:
        res = 0
    
    return res

if __name__ == "__main__":
    s = input().strip()
    result = camelcase(s)
    print(result)

Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        
        if(s.isEmpty())
        {
            System.out.println("0");
            System.exit(0);
        }
        
        int words = 1;
        
        for(char letter : s.toCharArray())
        {
            if(letter < 91 && letter > 64 )
            {
                words++;
            }
        }
        System.out.println(words);
    }
}

Note: This problem (CamelCase) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *