C++: Replacing Characters in A String

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

The discussion focuses on replacing spaces in a 2-character string variable named passCode in C++. The provided sample program initializes passCode with the value "1 ". Participants suggest checking each character of the string to determine if it is a space and replacing it with an underscore ('_'). The solution involves direct character manipulation within the string.

PREREQUISITES
  • Basic understanding of C++ syntax and string manipulation
  • Familiarity with character arrays and indexing in C++
  • Knowledge of conditional statements in C++
  • Experience with input and output operations in C++
NEXT STEPS
  • Learn about C++ string methods for character replacement
  • Explore C++ conditional statements for character checking
  • Investigate string indexing and manipulation techniques in C++
  • Practice writing C++ programs that modify strings based on user input
USEFUL FOR

C++ beginners, students learning string manipulation, and developers looking to enhance their skills in character handling within strings.

ineedhelpnow
Messages
649
Reaction score
0
Replace any space ' ' by '_' in 2-character string passCode.

Sample program:

Code:
#include 
#include 
#include 
using namespace std;

int main() {
   string passCode;

   passCode = "1 ";
   
<student code>

   

   cout << passCode << endl;
   return 0;
}

I'm super lost on this. I could definitely use some help/hints. Thanks.
 
Technology news on Phys.org
Check if the 0th character is a space. If so, assign [m]'_'[/m] to it. Do the same with the 1st character.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · 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