Finding Characters in Strings: timesFound() Function

In summary, the conversation discusses an assignment for a CS class that involves creating a function that counts the number of times a specific character appears in a string. The function should take in a string and a target character, and return the number of times the character appears in the string. The conversation also includes a code example for implementing the function and a discussion on how to approach the problem.
  • #1
ZakAttk1
7
0
I have an assignment for my CS class.

It is the following:

*
* complete the function:
*
* timesFound(string searchIn, char searchFor);
*
* The function should return the number of times the
* character searchFor appears in the string searchIn.
*
*/
int timesFound(string searchIn, char searchFor)
{
}

int main()
{
string textLine;
char findThis, response = 'y';
bool running = true;

while (running)
{
cout << "Please enter a line of text: ";
getline(cin, textLine);
cout << "Please enter the character to search for: ";
cin >> findThis;

cout << "The search character was found " << timesFound(textLine, findThis) << " times.\n";

cout << "Would you like to continue? (y/n) ";
cin >> response;

while (response != 'n' && response != 'y')
{
cout << "Would you like to continue? (y/n) ";
cin >> response;
}

if (response == 'n')
running = false;
else
cin.ignore();

}

}

As always I know what the program is supposed to do, but I am lost on how to start. Any help is appreciated. Thank you
 
Technology news on Phys.org
  • #2
Try putting your string into an array of chars and test each index. Let us see your attempt.
 
  • #3
Well, this isn't the homework forum, so I'm going to take whoever at his word and assume he just wants to know for his own curiosity. Maybe he's learning to program on his own... well, either way, I am going to treat him like an adult and just answer the guy's question.

Alright, so the "string" class in C++ contains an overloaded operator for array-access, the brackets. []

You can use this to check the value of individual characters in the string; so loop over the string's length, checking each character, and if it's equal to the target, increment the counter by one. For instance,

int timesFound(string searchIn, char searchFor)
{
int result = 0;

for(int i = 0; i < searchIn.length(); i++)
{
if(string == searchFor) result++;
}

return result;
}
 

What is the timesFound() function and what does it do?

The timesFound() function is a method used to find the number of times a specific character or string of characters appears in a given string. It searches the string from left to right and counts the occurrences of the specified character or string.

What is the syntax for using the timesFound() function?

The syntax for using the timesFound() function is: timesFound(string, character/substring). The first parameter is the string in which we want to search for the character or substring, and the second parameter is the character or substring that we want to find the number of occurrences for.

Can the timesFound() function be used to search for multiple characters or substrings?

Yes, the timesFound() function can be used to search for multiple characters or substrings. You can use the function multiple times, each time searching for a different character or substring. Alternatively, you can also use the regular expression feature to search for multiple characters or substrings at once.

How does the timesFound() function handle case sensitivity?

The timesFound() function is case-sensitive, meaning that it will only count the occurrences of the specified character or substring if it matches the exact case. For example, if you search for "a" in the string "Apple", it will only count the lowercase "a" and not the uppercase "A". To make the function case-insensitive, you can use the regular expression feature with the "i" flag.

What happens if the character or substring is not found in the string?

If the character or substring is not found in the string, the timesFound() function will return 0, indicating that there are no occurrences of the specified character or substring in the string.

Similar threads

  • Programming and Computer Science
Replies
5
Views
879
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
2
Views
870
Replies
10
Views
955
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top