anonymous
2009-06-21 16:08:22 UTC
Enter a first and last name: Lloyd Fosdick
Fosdick, F.
This is what is expected:
Enter a first name and last name: Lloyd Fosdick
Fosdick, L.
Why is it not properly showing the abbreviated first name? Here is my code:
void reverse_name(char *name);
int read_line(char str[], int n);
void reverse_name(char *name)
{
char input[STR_LEN];
char *word = input;
while(*name != '\0' && *name != ' ') {
*word = *name;
word++;
name++;
}
*word = '\0';
}
int read_line(char str[], int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
if (i < n)
str[i++] = ch;
str[i] = '\0';
return i;
}
int main()
{
char str[STR_LEN+1];
int i;
char period = '.';
char comma = ',';
printf("Enter a first and last name: ");
scanf ("%s", str);
scanf ("%c", &str[i]);
reverse_name(str);
read_line(str, STR_LEN);
printf("%s%c %c%c\n", str, comma, str[i], period);
return 0;
}