HackerRank CamelCase Problem Solution
In this post, we will solve HackerRank CamelCase Problem Solution.
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 one Two Three
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.
Sample Input
saveChangesInTheEditor
Sample Output
5
Explanation
String s contains five words:
- save
- Changes
- In
- The
- Editor
CamelCase C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
char* s = (char *)malloc(100001 * sizeof(char));
scanf("%s",s);
int i=1,count = 1;
while(s[i]!='\0')
{
if(s[i]<91)
{
count = count +1;
}
i = i+1;
}
printf("%d",count);
return 0;
}
CamelCase C++ Solution
#include<bits/stdc++.h>
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout);
#define reset(a,d) memset(a,d,sizeof(a))
#define eef else if
#define sf scanf
#define pf printf
#define i64 long long
#define ui64 unsigned long long
#define CASE(i) printf("Case %I64d: ", i);
#define nl puts ("")
#define ex exit(0)
#define ff first
#define ss second
#define pb(a,b) a.push_back(b)
#define pb2(a,i,b) a[i].push_back(b)
#define mp make_pair
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define DIST(x1,x2, y1, y2) (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)))
#define DIST3D(x1,x2, y1, y2, z1, z2) (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)) + ((z1-z2)*(z1-z2)))
#define CLR(a) a.clear()
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x))
#define ALL(x) (x).begin(),(x).end()
#define LLA(x) x.rbegin(), x.rend()
#define SORT(v) sort(ALL(v))
#define ODD(x) (((x)&1)==0?(0):(1))
////============ CONSTANT ===============////
#define MAXLL 9223372036854775807
#define MINLL -9223372036854775808
#define MAXL 2147483647
#define MINL -2147483648
#define mx7 10000007
#define mx6 1000006
#define inf 1<<30 //infinity value
#define eps 1e-9
#define mxx 100010
#define mod 1000000007
#define PI acos(-1.0)
#define lft 2*root // For Segment Tree
#define rgt lft+1
////======User Define Function==========////
using namespace std;
typedef long long ll;
ll bigmod(ll a,ll b,ll m)
{
long long x=1,y=a;
while(b>0)
{
if(b & 1)
x=(x*y)%m;
y=(y*y)%m;
b>>=1;
}
return x;
}
ll gcd(ll a,ll b)
{
ll rem=0;
if(b==0)return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return (a/gcd(a,b))*b;
}
////========== End of Templates ==============////
int main()
{
string s;
ll c=1,i,l;
cin>>s;
l=s.size();
for(i=1;i<l;i++)
{
if(s[i]>=65&&s[i]<=90)c++;
}
cout<<c<<endl;
return 0;
}
CamelCase C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
public static int Run(string s)
{
var capCount = 0;
foreach (char c in s)
{
if (c >= 'A' && c <= 'Z') capCount++;
}
return capCount + 1;
}
public static void Main(string[] args)
{
var s = Console.ReadLine();
Console.WriteLine("{0}", Run(s));
}
}
CamelCase Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'camelcase' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING s as parameter.
*/
public static int camelcase(String s) {
// Write your code here
int numWords = s.length() > 0 ? 1 : 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isUpperCase(s.charAt(i))) {
numWords++;
}
}
return numWords;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = bufferedReader.readLine();
int result = Result.camelcase(s);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
CamelCase JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var input = readLine();
var cchar;
var count = 1;
for(var i = 0; i < input.length; i++) {
cchar = input.charAt(i);
if(cchar == cchar.toUpperCase()) {
count += 1;
}
}
console.log(count);
}
CamelCase Python Solution
#!/bin/python3
print(len(list(filter(lambda c: c.isupper(), input()))) + 1)
Other Solutions