質問

I am a beginner programmer taking a class and I cannot get my output strings to print with spaces in between words. Here is my code below. It is supposed to take a string that I input and either change to all caps or all lower case as I specify when I run the program. If I put in MY CODE DOES NOT WORK, it outputs mycodedoesnotwork. Why is it removing the spaces?

 1 #include <stdio.h>
 2 #include <assert.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5
 6
 7 int shout(char * msgIn, char * msgOut) {
 8
 9         if (!msgIn || !msgOut)
 10                 return -1;
 11         while (*msgIn != '\0') {
 12                 if ('a' <= *msgIn && *msgIn <= 'z')
 13                         *msgOut = *msgIn + ('A' - 'a');
 14                 else
 15                         *msgOut = *msgIn;
 16                 msgIn++;
 17                 msgOut++;
 18         }
 19         *msgOut = '\0';
 20
 21         return 0;
 22 }
 23
 24
 25 int whisper(char const * msgIn, char * msgOut) {
 26         if (!msgIn || !msgOut)
 27                 return -1;
 28         while (*msgIn != '\0') {
 29                 if ('A' <= *msgIn && *msgIn <= 'Z')
 30                         *msgOut = *msgIn + ('a' - 'A');
 31                 else
 32                         *msgOut = *msgIn;
 33                 msgIn++;
 34                 msgOut++;
 35         }
 36         *msgOut = '\0';
 37         return 0;
 38 }
 39
 40 int main(int argc, char ** argv) {
 41         char in[128], out[128];
 42         int i;
 43         for (i = 1; i < argc; i++) {
 44                 if (strcmp("-w", argv[i]) == 0)
 45                         while (scanf("%s", in) != EOF) {
 46                                 whisper(in, out);
 47                                 printf("%s", out);
 48                         }
 49                 else if (strcmp("-s", argv[i]) == 0)
 50                         while (scanf("%s", in) != EOF) {
 51                                 shout(in, out);
 52                                 printf("%s", out);
 53                         }
 54         }
 55         printf("\n");
 56         return 0;
 57 }

~

~

役に立ちましたか?

解決

The scanf calls are reading in just the words (no spaces) and you are not adding spaces back in when you output your strings.

If you don't mind a trailing space, just change lines 47 and 52 to printf("%s ", out)

他のヒント

while (scanf("%s", in) != EOF)==> scanf() takes input up to space and send to function

and then in next iteration again takes word after space.

You need to use fgets() instead.

It is not problem with your shout() or wisper() functions, but problem with scanf().

When you specify %s to read string, the string will terminate at any white space chars - space, tab etc.

And that will not be included in the string. So space that you enter between the strings are not stored in in variable.

You may want to think of different approach to solve that.

The space is a string terminator for scanf() and scanf() does not include it in the acquired string. man 3 scanf

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top