Skip to content

Instantly share code, notes, and snippets.

@webgtx
Created December 23, 2024 20:01
Show Gist options
  • Save webgtx/5635f05b388efdaa2ee10b3c0f9c3fb6 to your computer and use it in GitHub Desktop.
Save webgtx/5635f05b388efdaa2ee10b3c0f9c3fb6 to your computer and use it in GitHub Desktop.
How to remove newline from the string in C
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 512
int main(void)
{
char* string = malloc(sizeof(char) * BUFFER_SIZE);
if (!string) return 1;
strcpy(string, "pneumenoultramicroscopicsilicovolcanoconiosis\n");
// Remove new line from the string
string[strcspn(string, "\n")] = '\0';
puts(string);
free(string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment