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

Discussion Overview

The discussion revolves around implementing a function in C++ to replace all occurrences of a period in a string with an exclamation point. The context includes coding challenges and programming techniques related to string manipulation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant provides a partial implementation of the function and seeks help to complete the while loop for replacing periods with exclamation points.
  • Another participant questions the return value of the find method when the character is not found and suggests using it directly in the while loop condition.
  • A participant mentions that replacing characters in JavaScript is simpler and provides an example using a regular expression.
  • Another participant notes that using QString::replace from the Qt framework would also simplify the task in C++, highlighting the limitations of std::string functions.
  • A different participant proposes a simple traversal method to replace characters in the string and contrasts it with Java's immutable string behavior.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to solve the problem, with no consensus on a single method being superior. Some favor using the find method while others advocate for a simple traversal approach.

Contextual Notes

There are unresolved questions regarding the behavior of the find method and the limitations of std::string functions compared to other languages or frameworks.

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