F
Felipe Ribeiro
Hi everybody.
In one of my exercises I have to write a program that gets a name
entered by the user in the format "first_name last_name" and puts it
in the format "last_name, initial_of_the_first_name". A session with
the program would be like this:
----------------------------------------------------------------------
Enter a first and last name: Felipe Ribeiro
Ribeiro, F.
----------------------------------------------------------------------
The user may enter any number of spaces before the first name, between
the first and the last and after the last.
Now, to the program I wrote.
---------------------------------------------------------------------------------------
#include <stdio.h> /* printf, putchar, getchar */
int main(void)
{
char first_letter, last_name;
printf("Enter a first and last name: ");
/* The user may enter spaces before the first name */
scanf(" %c", &first_letter);
/* Go to the first space */
while (getchar() != ' ');
/* Go to the first non-space character and store it */
while ((last_name = getchar()) == ' ');
/* Print everything before reaching a space or a \n */
while (last_name != ' ' && last_name != '\n') {
putchar(last_name);
last_name = getchar();
}
printf(", %c.\n", first_letter);
return 0;
}
---------------------------------------------------------------------------------------
My question is if there's a better way to make the program reach the
first letter of the last name. I don't know if those 2 whiles are a
good way to do it.
I'm just beginning with C so what's in the program is just about
everything I know. I don't know if there's a better way to write the
program with so little knowledge but I'd appreciate any good
advice.
Thanks in advance!
In one of my exercises I have to write a program that gets a name
entered by the user in the format "first_name last_name" and puts it
in the format "last_name, initial_of_the_first_name". A session with
the program would be like this:
----------------------------------------------------------------------
Enter a first and last name: Felipe Ribeiro
Ribeiro, F.
----------------------------------------------------------------------
The user may enter any number of spaces before the first name, between
the first and the last and after the last.
Now, to the program I wrote.
---------------------------------------------------------------------------------------
#include <stdio.h> /* printf, putchar, getchar */
int main(void)
{
char first_letter, last_name;
printf("Enter a first and last name: ");
/* The user may enter spaces before the first name */
scanf(" %c", &first_letter);
/* Go to the first space */
while (getchar() != ' ');
/* Go to the first non-space character and store it */
while ((last_name = getchar()) == ' ');
/* Print everything before reaching a space or a \n */
while (last_name != ' ' && last_name != '\n') {
putchar(last_name);
last_name = getchar();
}
printf(", %c.\n", first_letter);
return 0;
}
---------------------------------------------------------------------------------------
My question is if there's a better way to make the program reach the
first letter of the last name. I don't know if those 2 whiles are a
good way to do it.
I'm just beginning with C so what's in the program is just about
everything I know. I don't know if there's a better way to write the
program with so little knowledge but I'd appreciate any good
advice.
Thanks in advance!