Count of camel case characters present in a given string
Last Updated :
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: 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++
#include <bits/stdc++.h>
using namespace std;
int countCamelCase(string& S)
{
int count = 0;
for ( int i = 0; S[i]; i++) {
if (S[i] >= 65 && S[i] <= 91) {
count++;
}
}
cout << count;
}
int main()
{
string S = "ckjkUUYII" ;
countCamelCase(S);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void countCamelCase(String S)
{
int count = 0 ;
for ( int i = 0 ; i < S.length(); i++)
{
if (S.charAt(i) >= 65 && S.charAt(i) <= 91 )
{
count++;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
String S = "ckjkUUYII" ;
countCamelCase(S);
}
}
|
C#
using System;
public class GFG {
static void countCamelCase(String S) {
int count = 0;
for ( int i = 0; i < S.Length; i++) {
if (S[i] >= 65 && S[i] <= 91) {
count++;
}
}
Console.WriteLine(count);
}
public static void Main(String[] args) {
String S = "ckjkUUYII" ;
countCamelCase(S);
}
}
|
Python3
def countCamelCase(S):
count = 0
for i in range ( len (S)):
if ( ord (S[i]) > = 65 and ord (S[i]) < = 91 ):
count + = 1
print (count)
if __name__ = = '__main__' :
S = "ckjkUUYII"
countCamelCase(S)
|
Javascript
<script>
function countCamelCase( S) {
var count = 0;
for (i = 0; i < S.length; i++) {
if (S.charAt(i).charCodeAt(0) >= 65 && S.charAt(i).charCodeAt(0) <= 91) {
count++;
}
}
document.write(count);
}
S = "ckjkUUYII" ;
countCamelCase(S);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int countCamelCase(string s)
{
int count = 0;
for ( int i = 0; s[i]; i++) {
if ( isupper (s[i])) {
count++;
}
}
cout << count;
}
int main()
{
string str = "ckjkUUYII" ;
countCamelCase(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isupper( char st) {
if (st>= 'A' && st<= 'Z' )
return true ;
else
return false ;
}
static void countCamelCase(String s)
{
int count = 0 ;
for ( int i = 0 ; i<s.length(); i++) {
char st = s.charAt(i);
if (isupper(st)) {
count++;
}
}
System.out.print(count);
}
public static void main(String[] args)
{
String str = "ckjkUUYII" ;
countCamelCase(str);
}
}
|
Python3
def countCamelCase(s):
count = 0
for i in range ( len (s)):
if ( ord (s[i])> = ord ( 'A' ) and ord (s[i])< = ord ( 'Z' )):
count + = 1
print (count)
str = "ckjkUUYII"
countCamelCase( str )
|
C#
using System;
public class GFG {
static bool isupper( char st) {
if (st >= 'A' && st <= 'Z' )
return true ;
else
return false ;
}
static void countCamelCase(String s)
{
int count = 0;
for ( int i = 0; i < s.Length; i++) {
char st = s[i];
if (isupper(st)) {
count++;
}
}
Console.Write(count);
}
public static void Main(String[] args) {
String str = "ckjkUUYII" ;
countCamelCase(str);
}
}
|
Javascript
<script>
function countCamelCase(s){
let count = 0
for (let i=0;i<s.length;i++){
if (s.charCodeAt(i)>= 'A' .charCodeAt(0) && s.charCodeAt(i)<= 'Z' .charCodeAt(0))
count += 1
}
document.write(count, "</br>" )
}
let str = "ckjkUUYII"
countCamelCase(str)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...