Originally Posted by
-EquinoX-
are you saying I should set i first to the length of the string?
is there no other easy way to do this, like my code above does the job.. but it just don't know when to stop..
keeps on reading from stdin, while I only want it to read from stdin only once, which is till I press enter
I mean I know how to do this the hard way, but I am challanged to do this with as less effort as I can using only the provided C functions
No.
Code:
//something like this:
char goOn;
i = 0;
do {
if(c == '\n') {
while(i) {
printf("%c", CharArray[i]);
CharArray[i--] = '\0';
}
}
else {
CharArray[i++] = c;
continue;
}
i = 0;
printf("\nEnter Another Word [y/n] ? ");
scanf(" %c", &goOn);
}while((goOn != 'n' && goOn != 'N') )
This is completely untested code, and appears more than you want, anyway. Perhaps better, would be to use scanf() to get a whole string? scanf("%200s", charArray);
Instead of a word, you get the whole sentence. Make Array large so it can't overflow it within reason.
That would simplify the above.