Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that formats a user entered name (consiting of a first and last

ID: 3823910 • Letter: W

Question

Write a program that formats a user entered name (consiting of a first and last name) and then displays it in the format last name, a comma, the first name initial, and a period. Your program should work as follows:

Enter the first and last name: James Smith

Formatted name: Smith, J.

Constraints:

Your program should use the following function to format the name:

void format_name (char *name);

The function takes as input the string entered by the user and then it modifies the string name to obey the formatting constraint indicated above.

Your program should use pointer arithmetic, not array subscripting, to process the string.

Modify the above program to work even if the user input includes extra spaces before the first name, between the first and last name, as well as after the last name. These spaces should not be printed in the formatted name.

Explanation / Answer

#include <stdio.h>

int main(void)

{

char first_initial, last_name;

printf(" Enter a first and last name: ");

scanf(" %c", &first_initial);

while (getchar() != ' ')

{

}

while ((last_name = getchar()) != ' ')

{

if (last_name == ' ')

else

printf("%c", last_name);

}      

printf(", %c. ", first_initial);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote