accepting single string with space

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
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: http://ideone.com/6aO2Gg

Edit: http://discuss.codechef.com/questions/38874/how-to-read-string-with-whitespace-in-c

//