📌  相关文章
📜  给定字符串中出现的驼峰字符数

📅  最后修改于: 2022-05-13 01:57:08.478000             🧑  作者: Mango

给定字符串中出现的驼峰字符数

给定一个字符串S ,任务是计算给定字符串中出现的驼峰式字符的数量。

例子:

方法:给定的问题可以通过遍历给定的字符串S并计算 ASCII 值在[65, 91]范围内的那些字符来解决。检查所有字符后,打印获得的总计数作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


C++
// C++ program for the above approach
 
#include 
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


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


输出:
5

时间复杂度: O(N)
辅助空间: O(1)

替代方法:想法是使用内置库函数isupper() 来检查字符是否为大写字母。请按照以下步骤解决问题:

  • 初始化一个变量,比如count0 ,它存储给定字符串中大写字母数量的计数。
  • 遍历字符串并为每个字符检查isupper(s[i])是否为真,然后增加count
  • 打印计数作为答案。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
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

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
输出:
5

时间复杂度: O(N)
辅助空间: O(1)