Omit new line in a String - C

Hello,

I want to omit a new line in a string, because I don't want to go to the newline.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
printf("Code\t\tName\t\tLogin\t\tLogout\t\tlogin\n");

	for (int i = 0; i < 5; i++)
	{
		printf("%d", code[i]);
		printf("\t\t");

		printf("%s", name[i]);  /// I don't want to go to the next line.
		printf("\t\t");

		printf("%d", logout[i]);
		printf("\t\t");

		printf("%d", pay[i]);
		printf("\t\t");


		printf("%d", login[i]);
		printf("\n");
	}
Line 8 does not print a newline unless you have a newline in name[i] itself.
If you read name in with fgets it may still have the newline at the end, in which case you need to remove it:

1
2
3
4
    char line[256];
    fgets(line, sizeof line, stdin);
    char *p = strchr(line, '\n');    // include <string.h>
    if (p) *p = '\0';

Topic archived. No new replies allowed.