C++程序 统计字符串中给定字符出现的次数

C++程序 统计字符串中给定字符出现的次数

给出一个字符串和一个字符,任务是制作一个函数,统计给定字符在字符串中出现的次数。

示例:

输入 : str = “geeksforgeeks”
c = ‘e’
输出 : 4
‘e’ 在 str 中出现了4次。

输入 : str = “abccdefgaa”
c = ‘a’
输出 : 3
‘a’ 在 str 中出现了3次。

实现:

// C++ program to count occurrences of a given
// character
#include <iostream>
#include <string>
using namespace std;
 
// Function that return count of the given
// character in the string
int count(string s, char c)
{
    // Count variable
    int res = 0;
 
    for (int i=0;i<s.length();i++)
 
        // checking character in string
        if (s[i] == c)
            res++;
 
    return res;
}
 
// Driver code
int main()
{
    string str= "geeksforgeeks";
    char c = 'e';
    cout << count(str, c) << endl;
    return 0;
}
// JAVA program to count occurrences
// of a character
 
class GFG
{
    // Method that return count of the given
    // character in the string
    public static int count(String s, char c)
    {
        int res = 0;
 
        for (int i=0; i<s.length(); i++)
        {
            // checking character in string
            if (s.charAt(i) == c)
            res++;
        }
        return res;
    }
     
    // Driver method
    public static void main(String args[])
    {
        String str= "geeksforgeeks";
        char c = 'e';
        System.out.println(count(str, c));
    }
}
# Python program to count occurrences
# of a given character
 
# Method that return count of the
# given character in the string
def count(s, c) :
     
    # Count variable
    res = 0
     
    for i in range(len(s)) :
         
        # Checking character in string
        if (s[i] == c):
            res = res + 1
    return res
     
     
# Driver code
str= "geeksforgeeks"
c = 'e'
print(count(str, c))
     
# This code is contributed by "rishabh_jain".
// C# program to count occurrences
// of a character
using System;
         
public class GFG {
     
    // Method that return count of the given
    // character in the string
    public static int count(string s, char c)
    {
        int res = 0;
 
        for (int i = 0; i < s.Length; i++)
        {
             
            // checking character in string
            if (s[i] == c)
            res++;
        }
         
        return res;
    }
     
    // Driver method
    public static void Main()
    {
        string str = "geeksforgeeks";
        char c = 'e';
         
        Console.WriteLine(count(str, c));
    }
}
 
// This code is contributed by Sam007.
<?php
// PHP program to count
// occurrences of a given
// character
 
// Function that return count of
// the given character in the string
function count(s,c)
{
     
    // Count variable
    res = 0;
 
    for (i = 0; is); i++)
 
        // checking character in string
        if (s[i] ==c)
            res++;
 
    returnres;
}
 
    // Driver Code
    str= "geeksforgeeks";
    c = 'e';
    echo count(str,c) ;
    return 0;
     
// This code is contributed by nitin mittal.
?>
<script>
// JavaScript program to count occurrences
// of a character using recursion
 
// Recursive function that returns count of
// the given character in the string
function count(str, char){
    if (str.length === 0) {
        return 0;
    }
    let first = str.charAt(0);
    let rest = str.slice(1);
    return (first === char) + count(rest, char);
}
 
// Driver code to test above function
let str = "geeksforgeeks";
let c = 'e';
console.log(count(str, c));
 
// This code is contributed by PrinciRaj1992
</script>```  

Output

4

Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(len)

#include<bits/stdc++.h;
using namespace std;
 
int countinString(char ch,string s)
{
    // base case;
    if (s.length() == 0)
        return 0;
        int count = 0;
 
    // checking if the first character of
    // the given string is that character
    // or not
    if (s[0] == ch)
        count++;
 
        // this will count the occurrence of
        // given character in the string
        // from index 1 to the last
        // index of the string
    count += countinString(ch, s.substr(1));
 
    return count;
}
 
int main(){
    string str = "geeksforgeeks";
    char c = 'e';
    cout<<(countinString(c, str));
}
 
// This code is contributed by shinjanpatra
/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
      static int countinString(char ch, String s)
    {
          //base case;
        if(s.length()==0)
            return 0;
          int count = 0;
       
          //checking if the first character of
          //the given string is that character
          //or not
          if(s.charAt(0)==ch)
          count++;
       
          //this will count the occurrence of
          //given character in the string
          //from index 1 to the last
          //index of the string
          count+=countinString(ch,s.substring(1));
       
           
        return count;
    }
    public static void main (String[] args) {
        String str= "geeksforgeeks";
        char c = 'e';
        System.out.println(countinString(c,str));
    }
}
def countinString(ch, s):
 
    # base case;
    if (len(s) == 0):
        return 0
    count = 0
 
    # checking if the first character of
    # the given string is that character
    # or not
    if (s[0] == ch):
        count += 1
 
    # this will count the occurrence of
    # given character in the string
    # from index 1 to the last
    # index of the string
    count += countinString(ch, s[1:])
 
    return count
     
str = "geeksforgeeks"
c = 'e'
print(countinString(c, str))
 
# This code is contributed by shinjanpatra
/*package whatever //do not write package name here */
using System;
public class GFG {
      static int countinString(char ch, String s)
    {
          // base case;
        if(s.Length == 0)
            return 0;
          int count = 0;
       
          // checking if the first character of
          // the given string is that character
          // or not
          if(s[0] == ch)
          count++;
       
          // this will count the occurrence of
          // given character in the string
          // from index 1 to the last
          // index of the string
          count += countinString(ch,s.Substring(1));
       
           
        return count;
    }
   
  // Driver code
    public static void Main(String[] args) {
        String str= "geeksforgeeks";
        char c = 'e';
        Console.WriteLine(countinString(c,str));
    }
}
 
// This code is contributed by umadevi9616
<script>
/*package whatever //do not write package name here */
 
    function countinString( ch, s)
    {
        // base case;
        if (s.length == 0)
            return 0;
        var count = 0;
 
        // checking if the first character of
        // the given string is that character
        // or not
        if (s[0] == ch)
            count++;
 
        // this will count the occurrence of
        // given character in the string
        // from index 1 to the last
        // index of the string
        count += countinString(ch, s.substring(1));
 
        return count;
    }
     
        var str = "geeksforgeeks";
        var c = 'e';
        document.write(countinString(c, str));
 
// This code is contributed by gauravrajput1
</script>```  

输出

4

时间复杂度:O(len) ,其中len为给定字符串的长度。

辅助空间:O(len) ,其中len为给定字符串的长度。

使用ArrayList和HashMap实现:

import java.util.*;
 
public class HelloWorld{
     public static void main(String []args){
        String str="Geeks for Geeks";
        ArrayList<Character> al=new ArrayList<>();
        // put each and every character in arraylist
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(!Character.isWhitespace(ch)){
                al.add(ch);
            }
        }
        HashMap<Character,Integer> hm=new HashMap<>();
       //Couting character in string
        for(int i=0;i<al.size();i++){
            hm.putIfAbsent(al.get(i),Collections.frequency(al,al.get(i)));  
        }
        System.out.println(hm);
     }
}  

输出

{r=1, s=2, e=4, f=1, G=2, k=2, o=1}

时间复杂度:O(n) ,其中n为给定字符串中非空字符的数量。

辅助空间:O(n) ,其中n为给定字符串中非空字符的数量。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C++ 示例