Question:
I am close to figuring out a C program about reversing names. Can anyone help me?
anonymous
2009-06-21 16:08:22 UTC
I am getting the follwing output.

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;
}
Four answers:
The Phlebob
2009-06-21 21:13:09 UTC
Your problem lies in this statement:



printf("%s%c %c%c\n", str, comma, str[i], period);



What value does i have at that point? It does not have the value of the last character in the string, nor does it have that in the second scanf() statement. i is not adjusted by the length of the string read into str in the first scanf(). In fact, since i was never assigned a value, it's likely -- but not guaranteed -- to have the value zero (0), pointing to the first character in str[] -- which is the F in Fosdick.



Hope that helps.
Matt
2009-06-21 17:11:53 UTC
I will not lie, I did not even read your code. I have written it myself using my own methods and functions.



Hope this can help, because it does work. I will include the code in the source, and a couple of examples here.



Enter a first and last name: lloyd fosdick

fosdick, l.



Enter a first and last name: Bob Hope

Hope, B.



Enter a first and last name: FirstName LastName

LastName, F.



Enter a first and last name: ab cd

cd, a



so it does seem to be working. Anyway, good luck
?
2016-11-15 12:03:42 UTC
properly the priority out of date Binding at i is which you declared as an area to the loop variable you ought to declare it lower back, you place ss-num while it fairly is definitely ss_num, elementary mistake everybody does it so do no longer loose any sleep over it. yet another situation is in my experience interpreting entries use a do whilst loop rather than a whilst loop. that ought that may assist you somewhat i'm on a working laptop or laptop without C++ compiler in any different case i could of debugged it for you. properly it fairly is my 10 cents, yet you ought to artwork out something.
anonymous
2009-06-21 16:37:59 UTC
Well if you do a search here on yahoo answers...this question has been asked and solved...



Why don't you do that first...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...