Skip to content

Instantly share code, notes, and snippets.

@TheCodeEngine
Created April 24, 2013 15:05
Show Gist options
  • Save TheCodeEngine/5452848 to your computer and use it in GitHub Desktop.
Save TheCodeEngine/5452848 to your computer and use it in GitHub Desktop.
C Delete the new line character from the end of a string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void string_removeNewlineAtEnd(char *string)
{
if ( string[strlen(string) - 1] == '\n' )
string[strlen(string) - 1] = '\0';
}
int main(int argc, char const *argv[])
{
char name[80];
fgets(name, sizeof(name), stdin);
string_removeNewlineAtEnd(name);
fprintf(stdofut, "String: %s\n", name);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment