Dada una string S , la tarea es contar el número de caracteres de mayúsculas y minúsculas presentes en la string dada.
El carácter de mayúsculas y minúsculas se define como el número de caracteres en mayúsculas en la string dada.
Ejemplos:
Entrada: S = “ckjkUUYII”
Salida: 5
Explicación:
Los caracteres de mayúsculas y minúsculas Camel presentes son U, U, Y, I e I.Entrada: S = “abcd”
Salida: 0
Enfoque: el problema dado se puede resolver recorriendo la string S dada y contando aquellos caracteres cuyo valor ASCII se encuentra sobre el rango [65, 91] . Después de comprobar todos los caracteres, imprima el recuento total obtenido como resultado.
A continuación se muestra la implementación del enfoque anterior:
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
C#
// 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
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.
Javascript
<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
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Enfoque alternativo: la idea es usar la función de biblioteca incorporada isupper() para verificar si el carácter es una letra mayúscula o no. Siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos contar como 0 que almacena el recuento del número de letras mayúsculas en la string dada.
- Recorra la string y para cada carácter verifique si isupper(s[i]) es verdadero, luego incremente el conteo .
- Imprime el conteo como la respuesta.
A continuación se muestra la implementación del enfoque anterior:
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; }
Java
// 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
Python3
# 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#
// 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
Javascript
<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
Complejidad temporal: O(N)
Espacio auxiliar: O(1)