I am trying to get some data from the user and send it to another function in gcc. The code is something like this.
printf("Enter your Name: ");
if (!(fgets(Name, sizeof Name, stdin) != NULL)) {
fprintf(stderr, "Error reading Name.\n");
exit(1);
}
However, I find that it has a newline \n character in the end. So if I enter John it ends up sending John\n. How do I remove that \n and send a proper string.
The elegant way:
The slightly ugly way:
The slightly strange way:
Note that the
strtokfunction doesn’t work as expected if the user enters an empty string (i.e. presses only Enter). It leaves the\ncharacter intact.There are others as well, of course.