The elegant way:
Name[strcspn(Name, "\n")] = 0;
The slightly ugly way:
char *pos; if ((pos=strchr(Name, '\n')) != NULL) *pos = '\0'; else /* input too long for buffer, flag error */
The slightly strange way:
strtok(Name, "\n");
Note that the strtok
function doesn’t work as expected if the user enters an empty string (i.e. presses only Enter). It leaves the \n
character intact.
There are others as well, of course.
Related Posts:
- How does strtok() split the string into tokens in C?
- How to convert an int to string in C?
- What is a string of hexadecimal digits?
- How to convert an int to string in C?
- What is newline character — ‘\n’
- How to convert a string to integer in C?
- How do I create an array of strings in C?
- Convert char to int in C and C++
- Why is the gets function so dangerous that it should not be used?
- What is the difference between char s[] and char *s?
- How do I properly compare strings in C?
- How to convert an int to string in C?
- warning: incompatible implicit declaration of built-in function ‘xyz’
- Returning string from C function
- Why do I get an assertion failure?
- How do I create an array of strings in C?
- Difference between scanf() and fgets()
- Return char[]/string from a function [duplicate]
- How to check if a string is a number?
- How to read from stdin with fgets()?
- Reading \r (carriage return) vs \n (newline) from console with getc?
- How do I lowercase a string in C?
- conflicting types error when compiling c program using gcc
- What is a file with extension .a?
- Return char[]/string from a function
- How to clear all the elements of array in C?
- How do I concatenate const/literal strings in C?
- Reversing a string in C
- How to remove the character at a given index from a string in C?
- Split string with delimiters in C
- (.text+0x20): undefined reference to `main’ and undefined reference to function
- Error: initializer element is not computable at load time
- Allocating string with malloc
- What happens during a “relocation has invalid symbol index” error?
- What does this GCC error “… relocation truncated to fit…” mean?
- Does C have a string type?
- How to concatenate string and int in C?
- Reaching EOF with fgets
- The difference between char * and char[] [duplicate]
- uint8_t vs unsigned char
- What is the difference between ++i and i++?
- Using boolean values in C
- What does “collect2: error: ld returned 1 exit status” mean?
- typedef struct vs struct definitions [duplicate]
- What can be the reasons of connection refused errors?
- Why does ENOENT mean “No such file or directory”?
- Java String new line
- What does the question mark character (‘?’) mean?
- Cannot figure out how to use getchar(); in C
- c stack smashing detected
- Strings and character with printf
- What does “collect2: error: ld returned 1 exit status” mean?
- Warning comparison between pointer and integer
- Using ssize_t vs int
- Why a warning of “control reaches end of non-void function” for the main function?
- How to remove .html from URL?
- C read file line by line
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- where does stdio.o live in linux machine?
- Using Dynamic Memory allocation for arrays
- Get the current time in C
- error: indirection requires pointer operand (‘int’ invalid)
- What is the difference between exit(0) and exit(1) in C?
- error: indirection requires pointer operand (‘int’ invalid)
- Finding the length of a Character Array in C
- Valgrind: Invalid read of size 1
- Zero an array in C code
- Excess elements in char array initializer error
- Encrypt with private key and decrypt with public key
- strcmp giving segmentation fault
- What does “pointer being freed was not allocated” mean exactly?
- How to use timer in C?
- C char* to int conversion
- What is the difference between stdin and STDIN_FILENO?
- Debug vs Release in CMake
- How to simply convert a float to a string in c?
- What is the difference between char array and char pointer in C?
- Simple way to check if a string contains another string in C?
- Difference between fgets and fscanf?
- gcc -g :what will happen
- What are .axf files?
- printf format specifiers for uint32_t and size_t
- How to remove first character from C-string?
- What is an simple example of copy_from_user
- What is `S_ISREG()`, and what does it do?
- typedef struct pointer definition
- What is a quick way to force CRLF in C# / .NET?
- Math constant PI value in C
- The code does not execute properly. Try to figure out why
- What are “prototypes” in a C program?
- ld.exe: cannot open output file … : Permission denied
- How to read from input until newline is found using scanf()?
- What’s the difference between “mod” and “remainder”?
- What is char ** in C? [duplicate]
- undefined reference to `std::ios_base::Init::Init()’
- Fatal error: iostream: No such file or directory in compiling C program using GCC
- segmentation fault using scanf
- GCC: Array type has incomplete element type
- segmentation fault with strcpy [duplicate]