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;
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top