Working with char

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!
 
N

Nate Eldredge

Felipe Ribeiro said:
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. :)

I think what you've got is reasonable.

The loop doesn't seem offensive to me. There are certainly other ways
of doing this, but this seems fine for a beginner. Over time you will
learn more elegant techniques.

One thing you might take as an exercise is to make the program more
robust in the face of erroneous input. For instance, consider what
happens if the user ends the input without entering a newline, or
without a last name, or with no first name, or with no input at all.

Good luck with C!
 
G

Guest

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: ");

fflush(stdout) ensure the prompt appears (without a \n
or fflush() it may not on many implementations)


        /* 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. :)

I must admit I'd read both names using scanf() (or fegts() followed
by sscanf()) then use printf() to print the answer
 
N

Nate Eldredge

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:
[...]
I must admit I'd read both names using scanf() (or fegts() followed
by sscanf()) then use printf() to print the answer

How will you know how much space to allow?
 
B

BartC

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:
---------------------------------------------------------------------------------------
#include <stdio.h> /* printf, putchar, getchar */ .....

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.

Your approach seems adequate. I had a go myself and found the logic tough
(far too early in the morning for me). My attempt is below. It tries to use
a single while loop, relying on a state variable to figure out what it's
looking for.

The logic here allows for spaces in the last name (eg. john le carre), which
is a bit more fiddly but might get you an extra mark. Won't work if there
also middle names, it becomes more difficult then.

#include <stdio.h>
#include <string.h>

#define namemax 100

char firstname[namemax]={0};
char lastname[namemax]={0};

enum {leading,first,between,last} state;

int main(void){
char fullname[namemax];
char *source,*dest;
char c;

printf("Please enter first and last name: ");
fgets(fullname,namemax,stdin);

//printf("Full name: <%s>\n",fullname);

/*loop over input, using four states, and storing into
firstname or lastname */

state=leading;
source=fullname;
dest=NULL;

while (1) {
c=*source++;

if (c=='\n' || c==0) {
if (dest) *dest=0;
break;
}
switch (state) {
case leading: /* Looking for start of first name */
if (c>' ') {
state=first;
dest=firstname;
*dest++=c;
};
break;
case first: /* Reading first name, looking for space */
if (c==' ' || c=='\t') { /* Or pehaps c<=' ' to match rest */
*dest=0;
state=between;
}
else
*dest++=c;
break;
case between: /* Looking for start of last name */
if (c>' ') {
state=last;
dest=lastname;
*dest++=c;
}
break;
case last: /* Reading last name */
*dest++=c;
}
}

//Remove trailing white-space from lastname
dest=lastname+strlen(lastname)-1;
while (dest>=lastname && (*dest==' ' || *dest=='\t')) *dest-- = 0;

//printf("First = <%s>\n",firstname);
//printf("Last = <%s>\n",lastname);

if (lastname[0]==0)
puts("<No last name!>");

else if (firstname[0]==0)
puts("<No first name!>");
else
printf("%s, %c.\n",lastname,firstname[0]);

}
 
F

Felipe Ribeiro

I think what you've got is reasonable.

The loop doesn't seem offensive to me.  There are certainly other ways
of doing this, but this seems fine for a beginner.  Over time you will
learn more elegant techniques.

One thing you might take as an exercise is to make the program more
robust in the face of erroneous input.  For instance, consider what
happens if the user ends the input without entering a newline, or
without a last name, or with no first name, or with no input at all.

Good luck with C!

I suppose I should treat erros caused when the user doesn't enter
proper input but I'm not quite sure if I can accomplish that with the
little knolewdge I have. :)
I'll try to do it anyway. It might be a good experience.

Thank you for your response!
 
B

Barry Schwarz

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);

But this picks up the first character regardless if it is a space or
not.

If you deleted the scanf and changed first_letter to an int, then
while ((first_letter = getchar()) == ' ');
will save the first non-blank, similar to how you handle last_name
below.
/* Go to the first space */
while (getchar() != ' ');
/* Go to the first non-space character and store it */
while ((last_name = getchar()) == ' ');

getchar returns an int. It may not matter in this particular exercise
since you are not trying to handle the various "errors" that can occur
but it is a good habit to get into.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top