Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Count of camel case characters present in a given string

  • Last Updated : 28 Jul, 2021

Given a string S, the task is to count the number of camel case characters present in the given string.

The camel case character is defined as the number of uppercase characters in the given string.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:



Input: S = “ckjkUUYII”
Output: 5
Explanation: 
Camel case characters present are U, U, Y, I and I.

Input: S = “abcd”
Output: 0

Approach: The given problem can be solved by traversing the given string S and count those characters whose ASCII value lies over the range [65, 91]. After checking for all the characters, print the total count obtained as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count all the camelcase
// characters in the string S
int countCamelCase(string& S)
{
    // Stores the total count of
    // camelcase characters
    int count = 0;
 
    // Traverse the string S
    for (int i = 0; S[i]; i++) {
 
        // If ASCII value of character
        // lies over the range [65, 91]
        // then increment the count
        if (S[i] >= 65 && S[i] <= 91) {
            count++;
        }
    }
 
    // Print the total count obtained
    cout << count;
}
 
// Driver Code
int main()
{
    string S = "ckjkUUYII";
    countCamelCase(S);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to count all the camelcase
// characters in the string S
static void countCamelCase(String S)
{
     
    // Stores the total count of
    // camelcase characters
    int count = 0;
 
    // Traverse the string S
    for(int i = 0; i < S.length(); i++)
    {
         
        // If ASCII value of character
        // lies over the range [65, 91]
        // then increment the count
        if (S.charAt(i) >= 65 && S.charAt(i) <= 91)
        {
            count++;
        }
    }
 
    // Print the total count obtained
    System.out.println(count);
}
 
// Driver code
public static void main(String[] args)
{
    String S = "ckjkUUYII";
     
    countCamelCase(S);
}
}
 
// This code is contributed by Potta Lokesh

Python3




# Python 3 program for the above approach
 
# Function to count all the camelcase
# characters in the string S
def countCamelCase(S):
   
    # Stores the total count of
    # camelcase characters
    count = 0
 
    # Traverse the string S
    for i in range(len(S)):
        # If ASCII value of character
        # lies over the range [65, 91]
        # then increment the count
        if (ord(S[i]) >= 65 and ord(S[i]) <= 91):
            count += 1
 
    # Print the total count obtained
    print(count)
 
# Driver Code
if __name__ == '__main__':
    S = "ckjkUUYII"
    countCamelCase(S)
     
    # This code is contributed by ipg2016107.
Output: 
5

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Alternate Approach: The idea is to use the inbuilt library function isupper() to check whether the character is an uppercase letter or not. Follow the steps below to solve the problem: 

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of
// camelcase characters
int countCamelCase(string s)
{
    // Stores the count of all the
    // camelcase characters
    int count = 0;
 
    // Traverse the string S
    for (int i = 0; s[i]; i++) {
 
        // Check if the character is
        // an uppercase letter or
        // not using isupper()
        if (isupper(s[i])) {
 
            // Increment the count
            count++;
        }
    }
 
    // Print the total count
    cout << count;
}
 
// Driver Code
int main()
{
    string str = "ckjkUUYII";
    countCamelCase(str);
 
    return 0;
}
Output: 
5

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :