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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top