Check string length and set string end
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[10];
int i;
printf("Enter a string: ");
fgets(str, 10, stdin);
/* remove newline, if present */
i = strlen(str) - 1;
if(str[i] =='\n')
str[i] = '\0';
printf("This is your string: %s", str);
return 0;
}
Related examples in the same category