23.8k views
2 votes
8.11 LAB: Count characters - functions Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday, the output is: 1 Ex: If the input is: z Today is Monday, the output is: 0 Ex: If the input is: n It's a sunny day, the output is: 2 Case matters. n is different than N. Ex: If the input is: n Nobody, the output is: 0 Your program must define and call the following function that returns the number of times the input character appears in the input string. int CountCharacters(char userChar, const string

2 Answers

9 votes

Final answer:

To count the number of times a character appears in a string, you can define a function that iterates over each character and compares it to the character being counted. If there is a match, a counter variable is incremented. The function then returns the value of the counter variable.

Step-by-step explanation:

In order to count the number of times a character appears in a string, we can define a function called CountCharacters that takes two parameters: the character to be counted and the string. The function will iterate over each character in the string and compare it to the character being counted. If there is a match, a counter variable will be incremented. Finally, the function will return the value of the counter variable, indicating the number of times the character appears in the string.

int CountCharacters(char userChar, const string& inputString) { int count = 0; for (int i = 0; i < inputString.length(); i++) { if (inputString[i] == userChar) { count++; } } return count; }

To use this function, you can call it with the desired character and string, and assign the return value to a variable. You can then print this variable to display the result.

int main() { char userChar; string inputString; cin >> userChar; cin.ignore(); getline(cin, inputString); int charCount = CountCharacters(userChar, inputString); cout << charCount << endl; return 0; }

User Amani Ben Azzouz
by
4.3k points
10 votes

Final answer:

To solve this problem, define a function called CountCharacters that takes a character and a string as input. The function should iterate through the string and count how many times the character appears. Return the count as the output. You can call this function in your main program to get the desired output.

Step-by-step explanation:

To solve this problem, you can define a function called CountCharacters that takes two parameters: a character (userChar) and a string. The function should iterate through each character in the string and compare it with the input character. If they match, a counter should be incremented. At the end, the function should return the value of the counter.

Here's an example implementation:

int CountCharacters(char userChar, const string inputString) {
int count = 0;

for (int i = 0; i < inputString.length(); i++) {
if (inputString[i] == userChar) {
count++;
}
}

return count;
}

You can call this function in your main program by passing the input character and string as arguments:

char userChar;
string inputString;

// Prompt the user for input

cout << "Enter a character: ";
cin >> userChar;
cout << "Enter a string: ";
cin.ignore();
getline(cin, inputString);

// Call the CountCharacters function
int result = CountCharacters(userChar, inputString);

cout << "The character '" << userChar << "' appears " << result << " times in the string." << endl;

This program first prompts the user to enter a character and a string. Then, it calls the CountCharacters function and stores the result in the variable 'result'. Finally, it prints out the number of times the character appears in the string.

User Imotov
by
4.1k points