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.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top