如何计算字符串中给定字符的出现次数

字符串是编程面试中一个非常重要的话题。在面试之前练习一些专注于字符串的编程问题是明智的。在本文中,您将学习如何计算字符串中某个字符出现的总次数。

理解问题的例子

示例 1 :让给定的字符串是“她在海边卖贝壳”,给定的字符是“s”。

海峡= “S都给ELLššEA地狱S按在S EA小号·霍尔”

ch = 's'

在给定的字符串中,字符s出现了八次。

因此,输出为 8。

示例 2 :让给定的字符串是“他投了三个罚球”,给定的字符是“e”。

str = "H e thr e w th ee自由投掷"

ch = 'e'

在给定的字符串中字符e出现了 6 次。

因此,输出为 6。

计算字符串中字符总出现次数的方法

您可以按照以下方法查找字符串中某个字符的总出现次数:

  1. 初始化计数器变量以存储字符串中字符的总出现次数。
  2. 逐个字符遍历字符串。
  3. 如果字符串的字符与给定的字符匹配,则增加计数变量的值。
  4. 最后,返回计数器变量。

计算一个字符在字符串中出现的总次数的 C++ 程序

下面是计算一个字符在字符串中出现的总次数的 C++ 程序:

 // C++ program to count occurrences
// of a given character in a string
#include <iostream>
#include <string>
using namespace std;
// Function to count the occurrences of
// the given character in the string
int countOccurrences(string str, char ch)
{
// Counter variable
int counter = 0;
for (int i = 0; i < str.length(); i++)
{
// check if the given character matches
// with the character in the string
if (str[i] == ch)
{
// if the given character matches with
// the character in the string,
// increment the counter variable
counter++;
}
}
return counter;
}
// Driver code
int main()
{
string str1 = "she sells seashells by the seashore";
char ch1 = 's';
cout << "Input string 1: " << str1 << endl;
cout << "Character " << ch1 << " has occurred " <<
countOccurrences(str1, ch1) << " times in the given string." << endl;
string str2 = "peter piper picked a peck of pickled peppers";
char ch2 = 'p';
cout << "Input string 2: " << str2 << endl;
cout << "Character " << ch2 << " has occurred " <<
countOccurrences(str2, ch2) << " times in the given string." << endl;
string str3 = "I saw Susie sitting in a shoeshine shop";
char ch3 = 'a';
cout << "Input string 3: " << str3 << endl;
cout << "Character " << ch3 << " has occurred " <<
countOccurrences(str3, ch3) << " times in the given string." << endl;
string str4 = "Near an ear, a nearer ear, a nearly eerie ear";
char ch4 = 'r';
cout << "Input string 4: " << str4 << endl;
cout << "Character " << ch4 << " has occurred " <<
countOccurrences(str4, ch4) << " times in the given string." << endl;
string str5 = "He threw three free throws";
char ch5 = 'e';
cout << "Input string 5: " << str5 << endl;
cout << "Character " << ch5 << " has occurred " <<
countOccurrences(str5, ch5) << " times in the given string." << endl;
return 0;
}

输出:

 Input string 1: she sells seashells by the seashore
Character s has occurred 8 times in the given string.
Input string 2: peter piper picked a peck of pickled peppers
Character p has occurred 9 times in the given string.
Input string 3: I saw Susie sitting in a shoeshine shop
Character a has occurred 2 times in the given string.
Input string 4: Near an ear, a nearer ear, a nearly eerie ear
Character r has occurred 8 times in the given string.
Input string 5: He threw three free throws
Character e has occurred 6 times in the given string.

计算字符串中某个字符出现次数的 Python 程序

下面是计算字符串中字符出现总次数的 Python 程序:

相关:如何在 Python 中使用布尔方法验证字符串

# Python program to count occurrences
# of a given character in a string
# Function to count the occurrences of
# the given character in the string
def countOccurrences(str, ch):
# Counter variable
counter = 0
for char in str:
# check if the given character matches
# with the character in the string
if char == ch:
# if the given character matches with
# the character in the string,
# increment the counter variable
counter += 1
return counter
# Driver code
str1 = "she sells seashells by the seashore"
ch1 = 's'
print("Input string 1:", str1)
print("Character", ch1, "has occured",
countOccurrences(str1, ch1),"times in the given string.")
str2 = "peter piper picked a peck of pickled peppers"
ch2 = 'p'
print("Input string 2:", str2)
print("Character", ch2, "has occured",
countOccurrences(str2, ch2),"times in the given string.")
str3 = "I saw Susie sitting in a shoeshine shop"
ch3 = 'a'
print("Input string 3:", str3)
print("Character", ch3, "has occured",
countOccurrences(str3, ch3),"times in the given string.")
str4 = "Near an ear, a nearer ear, a nearly eerie ear"
ch4 = 'r'
print("Input string 4:", str4)
print("Character", ch4, "has occured",
countOccurrences(str4, ch4),"times in the given string.")
str5 = "He threw three free throws"
ch5 = 'e'
print("Input string 5:", str5)
print("Character", ch5, "has occured",
countOccurrences(str5, ch5),"times in the given string.")

输出:

 Input string 1: she sells seashells by the seashore
Character s has occurred 8 times in the given string.
Input string 2: peter piper picked a peck of pickled peppers
Character p has occurred 9 times in the given string.
Input string 3: I saw Susie sitting in a shoeshine shop
Character a has occurred 2 times in the given string.
Input string 4: Near an ear, a nearer ear, a nearly eerie ear
Character r has occurred 8 times in the given string.
Input string 5: He threw three free throws
Character e has occurred 6 times in the given string.

用于计算字符串中字符的总出现次数的 JavaScript 程序

下面是计算字符串中字符出现总次数的JavaScript程序:

 // JavaScript program to count occurrences
// of a given character in a string
// Function to count the occurrences of
// the given character in the string
function countOccurrences(str, ch)
{
// Counter variable
var counter = 0;
for (let i = 0; i < str.length; i++)
{
// check if the given character matches
// with the character in the string
if (str[i] == ch)
{
// if the given character matches with
// the character in the string,
// increment the counter variable
counter++;
}
}
return counter;
}
// Driver code
var str1 = "she sells seashells by the seashore";
var ch1 = 's';
document.write("Input string 1: " + str1 + "<br>");
document.write("Character " + ch1 + " has occurred " +
countOccurrences(str1, ch1) + " times in the given string." + "<br>");
var str2 = "peter piper picked a peck of pickled peppers";
var ch2 = 'p';
document.write("Input string 2: " + str2 + "<br>");
document.write("Character " + ch2 + " has occurred " +
countOccurrences(str2, ch2) + " times in the given string." + "<br>");
var str3 = "I saw Susie sitting in a shoeshine shop";
var ch3 = 'a';
document.write("Input string 3: " + str3 + "<br>");
document.write("Character " + ch3 + " has occurred " +
countOccurrences(str3, ch3) + " times in the given string." + "<br>");
var str4 = "Near an ear, a nearer ear, a nearly eerie ear";
var ch4 = 'r';
document.write("Input string 4: " + str4 + "<br>");
document.write("Character " + ch4 + " has occurred " +
countOccurrences(str4, ch4) + " times in the given string." + "<br>");
var str5 = "He threw three free throws";
var ch5 = 'e';
document.write("Input string 5: " + str5 + "<br>");
document.write("Character " + ch5 + " has occurred " +
countOccurrences(str5, ch5) + " times in the given string." + "<br>");

输出:

 Input string 1: she sells seashells by the seashore
Character s has occurred 8 times in the given string.
Input string 2: peter piper picked a peck of pickled peppers
Character p has occurred 9 times in the given string.
Input string 3: I saw Susie sitting in a shoeshine shop
Character a has occurred 2 times in the given string.
Input string 4: Near an ear, a nearer ear, a nearly eerie ear
Character r has occurred 8 times in the given string.
Input string 5: He threw three free throws
Character e has occurred 6 times in the given string.

相关:如何检查字符串是否为回文

解决问题的其他方法

您可以通过递归、正则表达式、库函数等其他方法来计算字符串中某个字符的总出现次数。本文中使用的迭代方法是解决此问题的最简单方法之一。

如果你想练习更多关于字符串的问题,请查看诸如如何反转字符串、如何检查字符串是否为回文、如何找到字符串中元音、辅音、数字和特殊字符的总数等问题,等等。