#include <stdlib.h> #include <string.h> #define MAX 100 void reverse_name(char *
ID: 3612585 • Letter: #
Question
#include <stdlib.h>
#include <string.h>
#define MAX 100
void reverse_name(char *name);
int read_line(char str[], intn);
int main()
{
char str[MAX+1];
int i = 0;
char period = '.';
char comma = ',';
printf("Enter a first and last name:");
scanf ("%s", str);
scanf ("%c", &str[i]);
reverse_name(str);
read_line(str, MAX);
printf("%s%c%c%c ", str, comma, str[i],period);
printf(" ");
system("PAUSE");
return 0;
}
int read_line(char str[], intn)
{
int ch, i = 0;
while ((ch = getchar()) !=' ')
{
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
void reverse_name(char *name)
{
char input[MAX];
char *word = input;
while(*name != '' && *name !='')
{
*word = *name;
word++;
name++;
}
*word = '';
}
Explanation / Answer
please rate - thanks changes in red. you forgot the space #include #include #include #include //needed for getch() #define MAX 100 void reverse_name(char *name); int read_line(char str[], int n); int main() { char str[MAX+1]; int i = 0; char period = '.'; char comma = ','; charspace,firstinitial; //added printf("Enter a first and last name: "); scanf ("%s", str); scanf("%c",&space); //forgot the space firstinitial=str[i]; //save first character //scanf("%c", &str[i]); //destroying first character-get rid of reverse_name(str); read_line(str, MAX); printf("%s%c %c%c ", str, comma, firstinitial,period); //print printf(" "); //system("PAUSE"); //this is C++ not C getch(); //use this for C - hangs a read return 0; } int read_line(char str[], int n) { int ch, i = 0; while ((ch = getchar()) != ' ') { if (iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.