0

I'm just trying to surround a name by a space and a star.

eg.

* John *

but the output I get is:

* John
 *

Why is this happening?

Thanks

#include<stdio.h> 

char name[30];

int main(int argc, char **argv) 
{ 

    printf ("Please enter your name: ");
    fgets ( name , 30 , stdin );

    printf ("* %s *", name);

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 5
    `fgets` will consume the `\n` from the input and put it into the string. – Eugene Sh. Jan 30 '20 at 22:23
  • Add `#include ` and after your `fgets()` call, add `name[strcspn(name, "\n")] = 0;` to trim the trailing `'\n'` from the end of `name` by overwriting the `'\n'` with the *nul-terminating* character `'\0'` (or just plain `0`).. – David C. Rankin Jan 30 '20 at 22:28
  • Thanks, this worked, also I found that when you just type `gets(name);` it also works without having to do that. – TrendyNiddrie Jan 31 '20 at 13:44

3 Answers3

2

The function fgets can append the new line character '\n' to the entered string.

You should remove it. You can do it the following way

#include <stdio.h>
#include <string.h>

//…

fgets ( name , 30 , stdin )
name[ strcspn( name, "\n" ) ] = '\0';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

fgets() is also getting the newline character you input in with enter. You just have to remove that \n. Just do name[strlen(name) - 1] = '\0'.

Warpstar22
  • 567
  • 4
  • 19
  • oops, I just typed in the wrong thing – Warpstar22 Jan 30 '20 at 22:28
  • You can always fix it – anastaciu Jan 30 '20 at 22:31
  • Also better to `size_t len = strlen(name);` then test that `len` is greater than `0` and that the character at the end is the `'\n'` character, e.g. `if [len && name[len-1] == '\n'] name[--len] = 0;` Otherwise if the line is the *empty-string* (no `'\n'`) you attempt to set `name[-1] = 0;` and that is bad... – David C. Rankin Jan 30 '20 at 22:32
  • @anastaciu, the person who commented before that deleted their comment after I fixed it – Warpstar22 Jan 30 '20 at 22:33
  • Ok, it's just a general advice, if it's like you want it to be, it's fine. – anastaciu Jan 30 '20 at 22:35
  • @Warpstar22 - you are free to draw from my comment to include that information in your answer if you like. If you do, drop a comment to me and I can come back and erase my comment. – David C. Rankin Jan 30 '20 at 22:40
0

Your call to fgets is also reading in the newline character when the user hits the enter key. You can try to use scanf:

scanf("%s", name);
  • Ooof..... (no dings), but generally that isn't what you think of as an improvement. You have 2 problems. (1) you fail to include the *field-width* modifier limiting the number of characters that can be read by `scanf` to `29` (no better than `gets()` without it), and (2) you now leave the `'\n'` in `stdin` unread. If the next call is a `fgets()` as well and you haven't extracted the `'\n'` from `stdin`, the next `fgets()` fails. Generally `fgets()` and trimming the `'\n'` is preferred over `scanf` for those reasons. – David C. Rankin Jan 30 '20 at 22:39