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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Global String
AI Thread 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.
 
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