fgets() inputs the entire line, including the trailing newline char.
After using the fgets(), you need to remove the newline char. You can use the following code:
Code:
char *p = NULL;
if ((p = strchr(id, '\n')) != NULL)
{
*p = '\0';
}
Other problems exist. Compare my version with yours:
Code:
#include <stdio.h>
#include <string.h>
int main (void)
{
char id[20];
char id2[20];
char *p = NULL;
printf("Enter your first name: ");
fgets(id, 20, stdin);
if ((p = strchr(id, '\n')) != NULL)
{
*p = '\0';
}
printf("%s\n", id);
printf("The number of letters entered is %zu \n", strlen(id));
printf("Enter your last name: ");
fgets(id2, 20, stdin);
if ((*p = strchr(id2, '\n')) != NULL)
{
*p = '\0';
}
printf("%s\n", id2);
printf("The number of letters entered is %zu \n", strlen(id2));
//From here I've tried numerous variations, but I'll leave the simplest one here
printf("%s\t%s\n", id, id2);
printf("The number of letters in first name is %zu \n", strlen(id));
printf("The number of letters in last name is %zu \n", strlen(id2));
printf("The number of letters in both names is %zu \n", strlen(id) + strlen(id2));
}