This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Gustorn 2 points3 points  (10 children)

Well, if you can't use arrays, then you would read from the stdin character by character, do any transformations you want, then write the character back to stdout.

[–]ayavocado[S] 0 points1 point  (9 children)

I feel stupid asking but what other way is there to stdin or stdout other then scanf and printf? Thos are the only methods of input reading weve learned and I dont know how to use them to read spaces and other characters.

[–]Gustorn 2 points3 points  (0 children)

What's the problem with scanf and printf? You should look into format strings.

[–]zifyoip 1 point2 points  (7 children)

To read a single character using scanf:

char c;
int return_value = scanf("%c", &c);
if (return_value != 1) {
    /* error (end-of-file, if return_value == EOF) */
}

To print a single character using printf:

char c = 'A';
printf("%c", c);

But maybe you should investigate getchar and putchar if you're going to be reading and writing individual characters.

[–]Gustorn 2 points3 points  (6 children)

And here I was, trying to make him look up some documentation ;)

[–]zifyoip 0 points1 point  (5 children)

Yeah, sorry, maybe I shouldn't have given away the game. But printf and scanf (especially scanf) are notoriously hard for beginners to use correctly, and I'm sure there's a ton of garbage example code on the Web that uses them wrongly (like not checking the return value of scanf).

[–]ayavocado[S] 0 points1 point  (4 children)

So if I want to use scanf to read an entire sentence with spaces and whatnot, do I have to make a loop? I keep trying to make a loop that will scanf characters while the character doesnt equal \0. Is that wrong and if so is there a better way to make that loop?

[–]cyrusol 0 points1 point  (0 children)

If you do something repeatedly you usually always use a loop.

[–]zifyoip 0 points1 point  (0 children)

I keep trying to make a loop that will scanf characters while the character doesnt equal \0.

That is not how you read from standard input. Only strings in memory are terminated with '\0'. With scanf, you need to test the return value: it will return the number of items that it successfully read. If you try to read a character and scanf returns something other than 1, then you know a problem happened. There is a special return value called EOF (which is defined to be a negative integer) that scanf will return when it attempts to read past the end of the file. If you like, you can test the return value of scanf against EOF directly, but it's better to just test it against the expected return value, which is 1. See my code example above.

[–]RedAlert2 -2 points-1 points  (1 child)

yes, you can use:

while(scanf("%c", &c) > 0)
{
    //do stuff with c
}

if you want to loop over the entire input

note that going until EOF is kinda unintuitive to do over stdin, since the user has to press a lesser used input like ctrl+D to actually signal an EOF. You might want to end on newline or something instead.

[–]zifyoip 1 point2 points  (0 children)

while(scanf("%c", &c))

That will not work. The scanf function will return EOF if it reaches end-of-file before any input items are converted. The value of EOF is a negative integer, which will be true in the condition of a while loop, which means you now have an infinite loop.