2

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 }

~

~

3
  • Argh! Please remove line numbers from code. Commented Oct 4, 2013 at 10:17
  • I figured it would help so someone could specify which line they were referring to but I'll note your request. Commented Oct 4, 2013 at 10:19
  • What do you think scanf does? Commented Oct 4, 2013 at 10:21

4 Answers 4

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the fix Oliver. That will do for my assignment due later today but I will check my undestanding of scanf and redoing it with a different approach.
1

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.

2 Comments

I'll look into this. The book has not yet gone over fgets() so it is implied that it must want it done with scanf. I'll look into this. Thank you for the tip.
its up to you. now just see the link.there you will found implementation.
0

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.

1 Comment

This is the only way the book has showed us to read strings and use them so it's my only approach. I'll look into fgets(). This book is a bit lacking. Thanks for the reply.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.