C++: Global replacement of a character within a string

In summary, the for loop iterates over the string looking for a character with a value of '.'. If it finds it, it replaces it with '!' and keeps going.
  • #1
ineedhelpnow
651
0
Complete the function to replace any period by an exclamation point. Ex: "Hello. I'm Miley. Nice to meet you." becomes:

"Hello! I'm Miley! Nice to meet you!"
Sample program:

#include <iostream>
#include <string>
using namespace std;

void MakeSentenceExcited(string& sentenceText) {
<STUDENT CODE>
}

int main() {
string testStr;

testStr = "Hello. I'm Miley. Nice to meet you.";
MakeSentenceExcited(testStr);
cout << testStr;

return 0;
}i came up with
int pos = sentenceText.find('.');
while()
{
sentenceText.replace(pos, 1, "!");
pos = sentenceText.find('.', pos+1);
}
but i don't know what goes int he while loop. please help. its almost due...
 
Technology news on Phys.org
  • #2
What does the find method return if the character is not found? You could also do this with a call to the find method in the condition of the while loop only.

By the way, this would be a snap in javascript:

[m]str = str.replace(/\./g, "!");[/m] :D
 
  • #3
MarkFL said:
By the way, this would also be a snap in javascript:

[m]str = str.replace(/\./g, "!");[/m] :D

If we use QString::replace from the Qt framework, this would be a snap in C++:

[m]str = str.replace(".", "!");[/m] ;)

Unfortunately the std::string from the Standard C++ Library offers a rather limited set of functions.
 
  • #4
Hi,
Unless this is an exercise in using the member functions of class string, I think a simple traversal of the string is probably better. As an old CS teacher, I believe the idea of traversal (iteration) of a data structure is best learned by first learning to traverse an array. Here's simple code:

Code:
void MakeSentenceExcited(string& sentenceText) {
    for (int i=0;i<sentenceText.length();i++) {
        if (string[i]=='.') {
            string[i]='!';
        }
    }
}

Btw, the above code wouldn't work in Java since Java Strings are immutable (const in C++ parlance). Also a Java String s has a method replace which returns a new string. So as above, one can make a one line method call.
 
  • #5


Hello! I'm Miley! Nice to meet you!

Great job on coming up with a solution for this problem! Your code is on the right track, but there are a couple of things that need to be fixed in order for it to work properly.

First, the while loop condition should be "pos != string::npos". This will ensure that the loop continues until all periods have been replaced.

Second, the "pos" variable should be updated within the loop to avoid an infinite loop. You can do this by setting "pos" equal to the result of "sentenceText.find('.', pos+1)".

Lastly, you can also add a check before the while loop to make sure that the string actually contains a period before attempting to replace it. This can be done by using an if statement with the condition "sentenceText.find('.') != string::npos".

With these changes, your code should look something like this:

int pos = sentenceText.find('.');
if (pos != string::npos) {
while (pos != string::npos) {
sentenceText.replace(pos, 1, "!");
pos = sentenceText.find('.', pos+1);
}
}

I hope this helps! Keep up the good work.
 

FAQ: C++: Global replacement of a character within a string

How do I replace all occurrences of a character in a string using C++?

To replace all occurrences of a character in a string using C++, you can use the standard library function std::replace. This function takes in three parameters: the beginning and end iterators of the string, and the character to be replaced. It will replace all instances of the character in the string with the specified replacement character.

Can I replace multiple characters in a string at once using C++?

Yes, you can replace multiple characters in a string at once using C++. Instead of using std::replace, you can use the standard library function std::replace_if, which takes in four parameters: the beginning and end iterators of the string, a function that determines which characters should be replaced, and the replacement character. This allows you to specify multiple characters to be replaced in one function call.

How do I replace a character at a specific position in a string using C++?

To replace a character at a specific position in a string using C++, you can use the std::string::replace function. This function takes in three parameters: the position of the character to be replaced, the number of characters to be replaced, and the replacement string. It will replace the specified number of characters at the given position with the provided string.

Is there a way to replace a character without modifying the original string in C++?

Yes, you can replace a character without modifying the original string in C++ by using the std::string::substr function. This function returns a substring of the original string, which you can then modify and use as needed.

Can I replace characters in a string based on a specific condition using C++?

Yes, you can replace characters in a string based on a specific condition using C++ by combining the functions std::string::find and std::replace. First, use std::string::find to find the position of the character you want to replace. Then, use std::replace to replace that character with the specified replacement character. You can also use std::string::find_first_of to find the first occurrence of any character in a given set, and then use std::replace to replace it.

Similar threads

Replies
22
Views
3K
Replies
8
Views
2K
Replies
1
Views
1K
Replies
40
Views
3K
Replies
118
Views
8K
Replies
10
Views
2K
Replies
89
Views
5K
Back
Top