Number of words in a camelcase sequence

CamelCase is the sequence of one or more than one words having the following properties:

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

Given a CamelCase sequence represented as a string. The task is to find the number of words in the CamelCase sequence.

Examples:



Input : str = "geeksForGeeks"
Output : 3

Input : str = "iGotAnInternInGeeksForGeeks"
Output : 8

Approach: As it is already known that the sequence is CamelCase, so it can be said that the number of words in the sequence will be one more than the number of uppercase letters.

  • Iterate the sequence from the 2nd letter to the end of the sequence.
  • No. of words will be equal to uppercase letters+1 during the 1st step iteration.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to find the count of words
// in a CamelCase sequence
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the count of words
// in a CamelCase sequence
int countWords(string str)
{
    int count = 1;
  
    for (int i = 1; i < str.length() - 1; i++) {
        if (isupper(str[i]))
            count++;
    }
  
    return count;
}
  
// Driver code
int main()
{
    string str = "geeksForGeeks";
  
    cout << countWords(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to find the count of words 
// in a CamelCase sequence 
class solution
{
  
// Function to find the count of words 
// in a CamelCase sequence 
static int countWords(String str) 
    int count = 1
  
    for (int i = 1; i < str.length() - 1; i++) { 
        if (str.charAt(i)>=65&&str.charAt(i)<=90
            count++; 
    
  
    return count; 
  
// Driver code 
public static void main(String args[]) 
    String str = "geeksForGeeks"
  
    System.out.print( countWords(str)); 
  
}
//contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find the count of words 
# in a CamelCase sequence 
  
# Function to find the count of words 
# in a CamelCase sequence 
def countWords(str):
    count = 1
    for i in range(1, len(str) - 1): 
        if (str[i].isupper()): 
            count += 1
  
    return count
  
# Driver code 
str = "geeksForGeeks"
print(countWords(str)) 
  
# This code is contributed 
# by sahishelangia

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to find the count of words 
// in a CamelCase sequence 
using System;
      
class GFG
{
  
// Function to find the count of words 
// in a CamelCase sequence 
static int countWords(String str) 
    int count = 1; 
  
    for (int i = 1; i < str.Length - 1; i++) 
    
        if (str[i] >= 65 && str[i] <= 90) 
            count++; 
    
  
    return count; 
  
// Driver code 
public static void Main(String []args) 
    String str = "geeksForGeeks"
  
    Console.Write(countWords(str)); 
  
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.