In: Computer Science
You must use C Language.
End Goal:
HATFIELD, HEIDI
KAISER, RUSSELL
LIPSHUTZ, HOWARD
PENKERT, DAWN
WRIGHT, ELIZABETH
The user inputs the students first name and last names separately but within one loop. The loop should end when the user presses enter on the first name without entering any text. Upon completing entry of data, the output pictured above should display on the output.
Using the code given, follow the steps:
1. You should be able to enter up to 20 student first names. Also, change the input array to an appropriate size of 18 for the length of the first name. Use a meaningful name for the storage of first names array. Change prompts as needed. The loop should exit when the user presses enter when inputing the first name without adding any text. Compile and make sure it works from main(). At this point, you should be able to enter and alphabetize a list of up to 20 first names! Alphabetizing the first name is just a test!!! In the end, you will alphabetize the whole name string.
2. Add another array and get input for last name INSIDE the loop for your first names. This last name array will also be an array of 20 elements but with room for up to 25 characters. Again, do not use another loop! Just add code to input the last name to the first loop. The program should now ask the user to input the student's first name and then last name in that order for each individual. Then the program will loop to continue adding student names until the user presses enter on the student's first name. Make sure the last name is converted to all caps. You do not need to alphabetize this array, but you may want to print it out to make sure everything is working just as a test.
3. Make changes to convert the first name to all upper case using a function. (Example: User enters bob on the first name, then on the last name enters jenkins, it will look like Bob Jenkins instead of bob jenkins)
Last step: Combine last and first into an third array. This code is most easily added to the first loop. You just had the user enter first and last names. So the current value of the subscript used for these arrays can be used to combine content and store in the third array. Alphabetize THIS array (instead of the first name array) which means you need to send a different pointer to the stsrt() function. Print out the end result. Test that everything is working on this program.
Given Code:
void rollsheet(void) {
int ct = 0;
char *ptstr[LIMIT];
char input[LIMIT][SIZE];
int k;
printf("Enter up to %d student names, and I will sort them!\n",
LIMIT);
printf("To stop, press the Enter key at a line's start.\n");
while (ct < LIMIT && s_gets(input[ct], SIZE) !=
NULL
&& input[ct][0] != '\0')
{
ptstr[ct] = input[ct]; /* set ptrs to strings */
ct++;
}
stsrt(ptstr,ct);
puts("\nHere's the sorted list:\n");
for (k = 0; k < ct; k++)
puts(ptstr[k]) ; /* sorted pointers */
}
void stsrt(char *strings[], int num)
{
char *temp;
int top, seek;
for (top = 0; top < num-1; top++)
for (seek = top + 1; seek < num; seek++)
if (strcmp(strings[top],strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}
Solution:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LIMIT 20
//Macro contains maximum limit of the
names...
#define FIRST_NAME_SIZE 20 //Macro contains maximum limit of the
names...
#define LAST_NAME_SIZE 25 //Macro contains maximum
size of the names...
char* toUpperCase(char *name);
void rollsheet(void);
char * s_gets(char * st, int n);
/*Declaring prototypes..*/
void stsrt(char *strings[], int num);
/**
* @Func : toUpperCase
* @brief: To convert the names to upper case..
*/
char* toUpperCase(char *name)
{
int i;
for(i=0; *(name + i)!='\0'; i++)
{
/* Checks each character is
lowercase alphabet */
if(*(name+i) >= 'a' &&
*(name+i) <= 'z')
name[i] = name[i]-32; /*Convert char to Upper
case*/
}
}//end..
/**
* @Func : rollsheet
* @brief: To manage the get and sort name functions..
* @return: void type(NULL)
*/
void rollsheet(void) {
int ct = 0;
char *ptstr[LIMIT];
char firstName[LIMIT][FIRST_NAME_SIZE];
/*Array to store firstNames..*/
char lastName[LIMIT][LAST_NAME_SIZE];
/*Array to store firstNames..*/
int k;
while (ct < LIMIT)
/*Loop to prompt user to
enter names..*/
{
printf("\nEnter the first name :
"); /*To get values from user..*/
if (s_gets(firstName[ct],
FIRST_NAME_SIZE) != NULL && firstName[ct][0] != '\0')
{
printf("Enter
the Last name : "); /*To get values from user..*/
if
(s_gets(lastName[ct], LAST_NAME_SIZE) != NULL &&
lastName[ct][0] != '\0')
{
toUpperCase(firstName[ct]);
/*Func call to convert to upper case..*/
toUpperCase(lastName[ct]);
/*Func call to convert to upper case..*/
strcat(firstName[ct], ", "); /*To
seperate first and last name..*/
strcat(firstName[ct], lastName[ct]); /*Combine
first and last name..*/
ptstr[ct] = firstName[ct]; /* Assign ptr to
names array */
}
else
break;
/*End of input..*/
}
else
break; /*End of
input..*/
ct++; /*Increment the pointer index*/
}//end_of_while..
stsrt(ptstr,ct);
/*Function call to sort the names..*/
printf("\n<---------------------------------------------------->\n\n");
puts("\nHere's the sorted list:\n");
for (k = 0; k < ct; k++)
puts(ptstr[k]) ;
/* Printing sorted array
elements(Output..)*/
}
/**
* @Func : stsrt
* @brief: Sorts the array in the alphabetic order..
* @return: void type(NULL)
*/
void stsrt(char *strings[], int num)
{
char *temp;
int top, seek;
/*Index variables..*/
for (top = 0; top < num-1; top++)
/*Loop to traverse array..*/
for (seek = top + 1; seek < num;
seek++)
if
(strcmp(strings[top],strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
/*Sorting by swaping*/
strings[seek] = temp;
}
}//end..
/**
* @Func : s_gets
* @brief: To get/read the names from user..
* @return: char* type(array)
*/
char * s_gets(char * st, int n)
{
char * ret_val;
/*Stores
the returning value..*/
int i = 0;
/*Index variable..*/
ret_val = fgets(st, n, stdin); /*Get
the names from user..*/
if (ret_val)
{
/*Loop to traverse
string..*/
while (st[i] != '\n' &&
st[i] != '\0')
i++;
if (st[i] == '\n') {
/*Check if end of line..*/
st[i] =
'\0';
} else {
/* should have word[i] ==
'\0'*/
while (getchar()
!= '\n')
continue;
}
}
return ret_val; /*
Returns the array value..*/
}
/**
* Main function
*/
int main()
{
printf("\n<---------------------------------------------------->\n\n");
printf("Enter up to limit(%d) student names, and it
will sorted.!!\n", LIMIT);
printf("To quit, press 'Enter' key at the fist line of
prompt..\n\n");
rollsheet();
/*Function call to get names...*/
printf("\n<---------------------------------------------------->\n\n");
return 0;
}
-----------------------------------------------------------------------
Code screenshots:




-----------------------------------------------------------------------
Output:

-----------------------------------------------------------------------
Kindly up-vote if you were satisfied with this solution.