So zählen Sie die Vorkommen eines bestimmten Zeichens in einer Zeichenfolge

Strings sind ein sehr wichtiges Thema bei der Programmierung von Interviews. Es ist ratsam, vor Ihren Interviews einige Programmierprobleme zu üben, die sich auf Strings konzentrieren. In diesem Artikel erfahren Sie, wie Sie die Gesamtzahl der Vorkommen eines Zeichens in einer Zeichenfolge zählen.

Beispiele zum Verständnis des Problems

Beispiel 1 : Die angegebene Zeichenfolge sei "sie verkauft Muscheln an der Küste" und das angegebene Zeichen sei 's'.

str = "s er s ell s s ea s Hölle s durch die s ea s hore"

ch = 's'

Es gibt acht Vorkommen des Zeichens s in der angegebenen Zeichenfolge.

Somit ist die Ausgabe 8.

Beispiel 2 : Die gegebene Zeichenfolge sei "Er warf drei Freiwürfe" und das angegebene Zeichen sei 'e'.

str = "H e thr thr ee e w fr ee wirft"

ch = 'e'

Das Zeichen e kommt in der angegebenen Zeichenfolge sechsmal vor.

Somit ist die Ausgabe 6.

Ansatz zum Zählen der Gesamtvorkommen eines Zeichens in einer Zeichenfolge

Sie können die Anzahl der Gesamtvorkommen eines Zeichens in einer Zeichenfolge ermitteln, indem Sie dem folgenden Ansatz folgen:

  1. Initialisieren Sie eine Zählervariable, um die Gesamtzahl der Vorkommen eines Zeichens in einer Zeichenfolge zu speichern.
  2. Durchlaufe die Zeichenfolge zeichenweise.
  3. Wenn das Zeichen der Zeichenfolge mit dem angegebenen Zeichen übereinstimmt, erhöhen Sie den Wert der Zählvariablen.
  4. Geben Sie schließlich die Zählervariable zurück.

C++-Programm zum Zählen der Gesamtvorkommen eines Zeichens in einem String

Unten ist das C++-Programm zum Zählen der gesamten Vorkommen eines Zeichens in einer Zeichenfolge:

 // 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;
}

Ausgabe:

 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-Programm zum Zählen der Gesamtvorkommen eines Zeichens in einer Zeichenfolge

Unten ist das Python-Programm zum Zählen der gesamten Vorkommen eines Zeichens in einer Zeichenfolge:

Verwandte: So validieren Sie Strings mit booleschen Methoden in 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.")

Ausgabe:

 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-Programm zum Zählen der Gesamtvorkommen eines Zeichens in einer Zeichenfolge

Unten ist das JavaScript- Programm zum Zählen der gesamten Vorkommen eines Zeichens in einer Zeichenfolge:

 // 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>");

Ausgabe:

 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.

Verwandte: So überprüfen Sie, ob eine Zeichenfolge ein Palindrom ist

Andere Methoden zur Lösung des Problems

Sie können die Anzahl der Gesamtvorkommen eines Zeichens in einer Zeichenfolge mit anderen Methoden wie Rekursion, regulären Ausdrücken, Bibliotheksfunktionen usw. ermitteln. Die in diesem Artikel verwendete iterative Methode ist eine der einfachsten Methoden, um dieses Problem zu lösen.

Wenn Sie weitere Probleme mit Strings üben möchten, sehen Sie sich Probleme an wie zum Beispiel, wie man einen String umkehrt, wie man prüft, ob ein String ein Palindrom ist, wie man die Gesamtzahl von Vokalen, Konsonanten, Ziffern und Sonderzeichen in einem String findet usw.