+ 3

C++ what is wrong in this code

This is a program to replace all the space with the astrick symbol https://code.sololearn.com/cGHy77t5hdo0/?ref=app

8th Jan 2018, 12:08 PM
DEEPENDU PANDIT
DEEPENDU PANDIT - avatar
5 Answers
+ 6
// There is no need to explicitly convert a string to a char array. Although that may be academically beneficial to understand. #include <iostream> #include <string> using namespace std; int main() { string n; cout<<"enter a string"; getline(cin, n); for(int i=0; i<n.length();i++) if (n[i]==' ') n[i]= '*'; cout<<n; return 0; }
8th Jan 2018, 12:42 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
You should edit your code by yourself, as it's your intellectual property. I, however, suggest a working algorithm: 1. Declare a string type variable 2. Take input 3. Convert the string to char array 4. Loop through the array and replace spaces with asterisks 5. Output the char array (Or you can convert char array back to string and output it) In case you didn't know: Convert string to char array https://stackoverflow.com/questions/13294067/how-to-convert-string-to-char-array-in-c Convert char array to string https://stackoverflow.com/questions/8960087/how-to-convert-a-char-array-to-a-string
8th Jan 2018, 12:41 PM
DAB
DAB - avatar
+ 3
n is not an array or a string in your code. Also, the condition part in the for loop is wrong. You should declare a string type variable, take input, convert the string to char array for using the approach you have used.
8th Jan 2018, 12:25 PM
DAB
DAB - avatar
+ 2
can you please edit it??
8th Jan 2018, 12:34 PM
DEEPENDU PANDIT
DEEPENDU PANDIT - avatar
+ 2
sololearn doesnt support some headers
8th Jan 2018, 12:38 PM
Kunal Kumar
Kunal Kumar - avatar