Finding Characters in Strings: timesFound() Function

  • Thread starter Thread starter ZakAttk1
  • Start date Start date
  • Tags Tags
    Function Strings
AI Thread Summary
The discussion revolves around a programming assignment for a computer science class, specifically the implementation of a function called `timesFound`, which counts the occurrences of a specified character within a given string. The user expresses uncertainty about how to begin coding the function. Participants suggest using C++'s string class, which allows for easy access to individual characters via array-like indexing. A proposed solution involves looping through the string's length, comparing each character to the target character, and incrementing a counter whenever a match is found. This approach provides a clear method for completing the assignment effectively.
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)
{
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
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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top