0

I want to create an empty string with fixed length lets say 100 and then enter user input for that string , For some reason when I try to scan the string the code isnt showing me all the leading empty spaces after the user input . If my empty string is of length 20 and input is 5 character then the count should be 20 . Am I missing something very basic here or the compiler truncates the empty spaces no matter what ?

int main () 
{
int count=0;
   char word[20];
   printf("Please enter a sting: ");
   scanf("%s", word);

   while (word[count]!= '\0' )
       {
       printf  ("%c",word[count]) ;
       count++;
       }
   printf ("\nCharacter count :%i\n", count);

}  

The output is :

Please enter a sting: Words
Words
Character count :5

Press any key to continue.
Fenomatik
  • 457
  • 2
  • 8
  • 22
  • 2
    Why should the count be `20` if the input is 5-long, again? `scanf` will terminate the inputted 5 characters with `\0`, not spaces. – Eugene Sh. Sep 15 '16 at 19:54
  • 4
    `scanf("%s", word);` skips leading whitespace, and stops at next whitespace. Time to read the man page? That is not the compiler's doing, it's the runtime code behaviour. The defined string length is only the *available* space - which will be overrun if you enter anything longer. – Weather Vane Sep 15 '16 at 19:55
  • @EugeneSh. Becuase I declared the string to be of length 20 and input is 5 , so there should be 20 in total . – Fenomatik Sep 15 '16 at 19:55
  • 2
    Looks like a serious misconception about strings and I/O. Joining @WeatherVane suggestion. – Eugene Sh. Sep 15 '16 at 19:57
  • @WeatherVane So how do I accomplish the objective of having a fixed length string and then I can fill it up with whatever I want leaving me with empty spaces in the end ? – Fenomatik Sep 15 '16 at 19:59
  • This is the difference between `strlen()` and `sizeof` a `char` array. – John Bollinger Sep 15 '16 at 20:00
  • @Fenomatik there is not only `scanf` function to read the input. check also getc, getchar, gets, fread... – Serge Sep 15 '16 at 20:03
  • @Serge I almost upvoted you, but you said `gets`, which must never, ever be used. This however↓, is upvote-worthy. – Medinoc Sep 15 '16 at 20:04
  • 2
    There may be a `scanf` format which will do that, others might advise, but you could also experiment with `fgets` using `stdin` which not only keeps leading and trailing spaces, but also the final `newline` which you would have to remove. – Weather Vane Sep 15 '16 at 20:05
  • 1
    as @Medinoc pointed out check also why you should avoid `gets`: http://stackoverflow.com/questions/22419866/is-the-gets-string-function-in-c-considered-a-bad-practice – Serge Sep 15 '16 at 20:11
  • 1
    If you want to print the string in a field of width 20, left justified, blank padded, then `printf("[%-20s]\n", "words");` will do the job. The square brackets make the result visible; the `-` is for left-justified; the 20 says the width must be at least 20 characters. If you want at most 20 characters, you can use `printf("[%-20.20s]\n", "abcdefghijklmnopqrstuvwxyz");` to limit the output to at most (as well as at least) 20 characters, left justified. See [`printf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html) for details. – Jonathan Leffler Sep 15 '16 at 20:18

1 Answers1

1

Typically scanf("%s", word); reads but does not save leading white-space and then saves non-white-space characters to word. It then appends a null character and returns.

To read a line of user input, including space character, use fgets(). This will usually include the Enter or '\n'.

 char word[20]; // Or maybe 100
 if (fgets(word, sizeof word, stdin)) {

   while (word[count]!= '\0') {
     printf("%c", word[count]);
     count++;
   }
   printf ("\nCharacter count :%i\n", count);
 }

If my empty string is of length 20 and input is 5 character then the count should be 20 .

The count will reflect the number of characters entered up to 20 - 1 or 19.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256