could anyone tell about accepting a string with space,especially inside a for loop.
i have tried getline,gets,but not getting desired output.
if i enter the string as: hey sum
it is read as 2 inputs because of space in between.
explanation with a code is needed.
thank you!
1 Like
r3gz3n
2
char c, s[100];
int i = 0;
scanf("%c", &c);
while (c != '\n')
{
s[i++] = c;
scanf("%c", &c);
}
#include
#include
using namespace std;
int main() {
char c, s[100];
int i = 0;
scanf("%c", &c);
while (c != '\n')
{
s[i++] = c;
scanf("%c", &c);
}
for(int j=0; j<i; j++) cout<<s[j];
printf("%s", s);
return 0;
}
however, only cout<<s;
or printf("%s", s);
wont be working properly, you have to use loop again.
btw, hit Enter at the end of string otherwise it gives RTE: 6aO2Gg - Online C++ Compiler & Debugging Tool - Ideone.com
Edit: How to read string with whitespace in C++? - general - CodeChef Discuss