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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Global String
Click For Summary
The discussion focuses on completing a C++ function that replaces periods in a string with exclamation points. A user shared an initial approach using the `find` method to locate periods and replace them, but sought help with the while loop condition. Participants noted that in C++, the `std::string` class has limited functions compared to other languages like JavaScript, where a simple regex replacement can be done easily. An alternative solution was proposed using a for loop to iterate through the string and replace characters directly, emphasizing the importance of understanding data structure traversal. Additionally, it was mentioned that Java's string immutability would prevent a similar approach, highlighting differences in string handling across programming languages.
ineedhelpnow
Messages
649
Reaction score
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
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
 
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.
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
9K
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
5
Views
2K
Replies
89
Views
6K