#string find and replace

13 messages · Page 1 of 1 (latest)

golden musk
#

i have to find and replace the string the user inputed but dont know how
#include <iostream>
#include <string>
// solution a

using namespace std;

string readSingleLineInputString, readSearchString, readReplacementString, replaceAllOccurrences;

int main(){

do{

cout << "Enter the single line input string: ";
getline(cin,readSingleLineInputString);

cout << "\n";

if(readSingleLineInputString.empty()){
cout << "Invalid input. Please enter a non-empty string." << endl << endl;
}
}
while (readSingleLineInputString.empty());

do{

cout << "Enter the search string: ";
getline(cin,readSearchString);

cout << "\n";

if(readSearchString.empty()){
cout << "Invalid input. Please enter a non-empty string." << endl << endl;
}
}
while (readSearchString.empty());

do{

cout << "Enter the replace string: ";
getline(cin,readReplacementString);

cout << "\n";

if(readReplacementString.empty()){
cout << "Invalid input. Please enter a non-empty string." << endl << endl;
}
}
while(readReplacementString.empty());

cout << "\n \n";

cout << "Output:";

return 0;

}

verbal ravineBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

golden musk
#

well also following these rules Task 4: Replace All Occurrences
• You must write a dedicated function to perform this step.
• Search for all occurrences of the search string within the input string.
• Replace each occurrence with the replacement string.
• Continue this process until all occurrences are replaced.
• Implement this functionality using a while loop.
• Use must use the find and replace string methods inside the function.
Function Signature:
string replaceAllOccurrences(string input, string search, string replacement)
Function Details:
• Receives three parameters:

  1. Input string
  2. Search string
  3. Replacement string
    • Returns the modified input string with all occurrences replaced.
tepid estuary
#

!code

verbal ravineBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
verbal ravineBOT
#

i have to find and replace the string the user inputed but dont know how

#include <iostream>
#include <string>
// solution a

using namespace std;

string readSingleLineInputString, readSearchString, readReplacementString,
    replaceAllOccurrences;

int main() {
  do {
    cout << "Enter the single line input string: ";
    getline(cin, readSingleLineInputString);

    cout << "\n";

    if (readSingleLineInputString.empty()) {
      cout << "Invalid input. Please enter a non-empty string." << endl << endl;
    }
  } while (readSingleLineInputString.empty());

  do {
    cout << "Enter the search string: ";
    getline(cin, readSearchString);

    cout << "\n";

    if (readSearchString.empty()) {
      cout << "Invalid input. Please enter a non-empty string." << endl << endl;
    }
  } while (readSearchString.empty());

  do {
    cout << "Enter the replace string: ";
    getline(cin, readReplacementString);

    cout << "\n";

    if (readReplacementString.empty()) {
      cout << "Invalid input. Please enter a non-empty string." << endl << endl;
    }
  } while (readReplacementString.empty());

  cout << "\n \n";

  cout << "Output:";

  return 0;
}
:atm:
tepid estuary
#

There's a bunch of ways to do this. I think it's better if you attempt it by yourself first and even if you end up with a wrong solution, people on this server will help you

#

because right now, you're only getting the inputs and nothing else

tepid estuary
#

Just copy and paste your code here after attempting to do it

verbal ravineBOT
#

@golden musk Has your question been resolved? If so, type !solved :)

tepid estuary
#

Don’t pay any mind to the bot right now. It generates this message I think whenever the OP says thank you