Finding Characters in Strings: timesFound() Function

  • Thread starter Thread starter ZakAttk1
  • Start date Start date
  • Tags Tags
    Function Strings
Click For Summary
SUMMARY

The forum discussion centers on implementing the timesFound(string searchIn, char searchFor) function in C++. This function is designed to count the occurrences of a specified character within a given string. A user provided a basic structure for the function, and a community member suggested using a loop to iterate through the string's characters, comparing each to the target character and incrementing a counter accordingly. The final implementation includes a loop that checks each character in the string and returns the total count.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with the string class in C++
  • Basic knowledge of loops and conditionals in programming
  • Experience with function creation and return values in C++
NEXT STEPS
  • Study C++ string manipulation techniques
  • Learn about array access and indexing in C++
  • Explore the use of loops and conditionals in C++ programming
  • Investigate best practices for function design in C++
USEFUL FOR

This discussion is beneficial for beginner C++ programmers, students completing assignments related to string manipulation, and anyone looking to enhance their understanding of character counting within strings.

ZakAttk1
Messages
7
Reaction score
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)
{
count << "Please enter a line of text: ";
getline(cin, textLine);
count << "Please enter the character to search for: ";
cin >> findThis;

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

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

while (response != 'n' && response != 'y')
{
count << "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
Try putting your string into an array of chars and test each index. Let us see your attempt.
 
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;
}
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
Replies
5
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
2
Views
2K
Replies
10
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K