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]
- How big can a 64 bit unsigned integer be?
- The difference between n++ and ++n at the end of a while loop? (ANSI C)
- Implementing Taylor Series for sine and cosine in C
- Working on code to calculate cosine with factorial sum
- How to use execvp()
- How to use execvp() to execute a command
- warning: implicit declaration of function
- warning: implicit declaration of function
- What is *(uint32_t*)?
- pthread_join() and pthread_exit()
- what is Segmentation fault (core dumped)? [duplicate]
- What causes a segmentation fault (core dump) to occur in C?
- uint8_t vs unsigned char
- How to use symbols of extended ASCII table in C?
- Two questions about basic C programs
- 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]
- How to print in C
- Why should we typedef a struct so often in C?
- Why should we typedef a struct so often in C?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is the difference between ++i and i++?
- What can be the reasons of connection refused errors?
- What does “collect2: error: ld returned 1 exit status” mean?
- typedef struct vs struct definitions [duplicate]
- 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
- Floating point exception (core dumped)
- Undefined reference to pthread_create in Linux
- what is the difference between uint16_t and unsigned short int incase of 64 bit processor?
- makefile:4: *** missing separator. Stop
- Stack smashing detected
- Why am I getting “void value not ignored as it ought to be”?
- Correct format specifier for double in printf
- Stack smashing detected
- How do I determine the size of my array in C?
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- dereferencing pointer to incomplete type
- 1 = false and 0 = true?
- c stack smashing detected
- Openssl : error “self signed certificate in certificate chain”
- What is the difference between C and embedded C?
- Get a substring of a char* [duplicate]
- char *array and char array[]
- Undefined reference to pthread_create
- What is the behavior of integer division?
- How do I break a string in YAML over multiple lines?
- Get a substring of a char*
- Difference between malloc and calloc?
- What is Bit Masking?
- How do function pointers in C work?
- Expression must be a modifiable L-value
- What causes the Broken Pipe Error?
- segmentation fault : 11
- Strings and character with printf
- Awesomium sdk download