+ 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
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;
}
+ 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
+ 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.
+ 2
can you please edit it??
+ 2
sololearn doesnt support some headers