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: 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++ 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 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 |
// C# program for the above approach using 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 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.
|
<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++ 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;
} |
// Java program for the above approach import 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 characters def 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 Code str = "ckjkUUYII"
countCamelCase( str )
# This code is contributed by shinjanpatra |
// C# program for the above approach using 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 characters function 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 Code let str = "ckjkUUYII"
countCamelCase(str) // This code is contributed by shinjanpatra </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(1)