Count of camel case characters present in a given string - GeeksforGeeks

04 May, 2022
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.
Examples:
Input: S = “ckjkUUYII”Output: 5Explanation: 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++
Java
C#
Python3
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to count all the camelcase// characters in the string Sint 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 Codeint main(){    string S = "ckjkUUYII";    countCamelCase(S);     return 0;}
// Java program for the above approachimport java.io.*; class GFG{     // Function to count all the camelcase// characters in the string Sstatic 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 codepublic static void main(String[] args){    String S = "ckjkUUYII";         countCamelCase(S);}} // This code is contributed by Potta Lokesh
// C# program for the above approachusing System;public 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[i] >= 65 && S[i] <= 91) {        count++;      }    }     // Print the total count obtained    Console.WriteLine(count);  }   // Driver code  public static void Main(String[] args) {    String S = "ckjkUUYII";     countCamelCase(S);  }} // This code is contributed by Rajput-Ji
# Python 3 program for the above approach # Function to count all the camelcase# characters in the string Sdef 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 Codeif __name__ == '__main__':    S = "ckjkUUYII"    countCamelCase(S)         # This code is contributed by ipg2016107.
<script>// javascript program for the above approach     // Function to count all the camelcase    // characters in the string S    function countCamelCase( S) {         // Stores the total count of        // camelcase characters        var count = 0;         // Traverse the string S        for (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).charCodeAt(0) >= 65 && S.charAt(i).charCodeAt(0)  <= 91) {                count++;            }        }         // Print the total count obtained        document.write(count);    }     // Driver code        S = "ckjkUUYII";        countCamelCase(S); // This code is contributed by Rajput-Ji</script>
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: 
Initialize a variable, say count as 0 that stores the count of the number of uppercase letters in the given string.
Traverse the string and for each character check if isupper(s[i]) is true, then increment the count.
Print the count as the answer.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to count the number of// camelcase charactersint 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 Codeint main(){    string str = "ckjkUUYII";    countCamelCase(str);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{  static boolean isupper(char st) {    if(st>='A' && st<='Z')      return true;    else      return false;  }   // Function to count the number of  // camelcase characters  static void countCamelCase(String s)  {    // Stores the count of all the    // camelcase characters    int count = 0;     // Traverse the String S    for (int i = 0; i<s.length(); i++) {       // Check if the character is      // an uppercase letter or      // not using isupper()      char st = s.charAt(i);      if (isupper(st)) {         // Increment the count        count++;      }    }     // Print the total count    System.out.print(count);  }   // Driver Code  public static void main(String[] args)  {    String str = "ckjkUUYII";    countCamelCase(str);  }} // This code is contributed by Rajput-Ji
# Python program for the above approach # Function to count the number of# camelcase charactersdef countCamelCase(s):       # Stores the count of all the    # camelcase characters    count = 0     # Traverse the string S    for i in range(len(s)):         # Check if the character is        # an uppercase letter or        # not using isupper()        if (ord(s[i])>=ord('A') and ord(s[i])<=ord('Z')):             # Increment the count            count += 1     # Print the total count    print(count) # Driver Codestr = "ckjkUUYII"countCamelCase(str) # This code is contributed by shinjanpatra
// C# program for the above approachusing System; public class GFG {  static bool isupper(char st) {    if (st >= 'A' && st <= 'Z')      return true;    else      return false;  }   // Function to count the number of  // camelcase characters  static void countCamelCase(String s)  {     // Stores the count of all the    // camelcase characters    int count = 0;     // Traverse the String S    for (int i = 0; i < s.Length; i++) {       // Check if the character is      // an uppercase letter or      // not using isupper()      char st = s[i];      if (isupper(st)) {         // Increment the count        count++;      }    }     // Print the total count    Console.Write(count);  }   // Driver Code  public static void Main(String[] args) {    String str = "ckjkUUYII";    countCamelCase(str);  }} // This code is contributed by gauravrajput1
<script> // JavaScript program for the above approach // Function to count the number of// camelcase charactersfunction countCamelCase(s){       // Stores the count of all the    // camelcase characters    let count = 0     // Traverse the string S    for(let i=0;i<s.length;i++){         // Check if the character is        // an uppercase letter or        // not using isupper()        if (s.charCodeAt(i)>='A'.charCodeAt(0) && s.charCodeAt(i)<='Z'.charCodeAt(0))             // Increment the count            count += 1    }     // Print the total count    document.write(count,"</br>")} // Driver Codelet str = "ckjkUUYII"countCamelCase(str) // This code is contributed by shinjanpatra </script>
5
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
lokeshpotta20
ipg2016107
Rajput-Ji
simranarora5sos
GauravRajput1
shinjanpatra
ASCII
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
String class in Java | Set 1
Print all subsequences of a string
sprintf() in C
How to Append a Character to a String in C
Convert character array to string in C++
Program to count occurrence of a given character in a string
Boyer Moore Algorithm for Pattern Searching