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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Global String
Click For Summary
SUMMARY

The discussion focuses on implementing a function in C++ to replace periods with exclamation points in a string. Participants provided various solutions, including using the find method and a simple traversal approach. The final recommended implementation iterates through the string and replaces each period with an exclamation point. Additionally, comparisons were made with Java and JavaScript, highlighting the differences in string mutability and available methods.

PREREQUISITES
  • Understanding of C++ string manipulation
  • Familiarity with the std::string class and its member functions
  • Knowledge of control structures such as loops and conditionals
  • Basic understanding of string immutability in Java
NEXT STEPS
  • Explore C++ string manipulation techniques using std::string
  • Learn about the find method and its return values in C++
  • Investigate string traversal methods in C++ for data structure education
  • Compare string handling in C++ with Java and JavaScript for better understanding of language differences
USEFUL FOR

Software developers, particularly those working with C++, educators teaching string manipulation concepts, and programmers interested in comparing string handling across different 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);
count << 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.
 

Similar threads

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