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

/* Create a program that prints a class roll sheet in alphabetical order. The us

ID: 3713413 • Letter: #

Question

/*
Create a program that prints a class roll sheet in alphabetical order. The user inputs the students' first name and last names (separately!), and presses enter on the first name when done. The program prints out the roster like this...

HATFIELD, HEIDI
KAISER, RUSSELL
LIPSHUTZ, HOWARD
PENKERT, DAWN
WRIGHT, ELIZABETH

Before starting, carefully study sort_str(), mod_str(), and format().
Make a copy of sort_str() and rename it roll(). Also, change stsrt(). Compile and make sure it works from main().
Change limit[] to represent first name. The input should work for 30 students and first name should be 15 characters. Change prompts as needed. Compile and test.
Make changes to convert the first name to all upper case using a function from mod_str().
Add another array and get input for last name which will also be an array of 30 with 15 characters.
Combine last and first into an third array. You may use sprintf() for this one. There are other ways.

*/

References:

################################################################

/* sort_str.c -- reads in strings and sorts them */

#include <stdio.h>

#include <string.h>

#define SIZE 81        /* string length limit, including */

#define LIM 20         /* maximum number of lines to be read */

#define HALT ""        /* null string to stop input          */

void stsrt(char *strings[], int num);/* string-sort function */

char * s_gets(char * st, int n);

void sort_str(void)

{

    char input[LIM][SIZE];     /* array to store input       */

    char *ptstr[LIM];          /* array of pointer variables */

    int ct = 0;                /* input count                */

    int k;                     /* output count               */

   

    printf("Input up to %d lines, and I will sort them. ",LIM);

    printf("To stop, press the Enter key at a line's start. ");

    while (ct < LIM && s_gets(input[ct], SIZE) != NULL

           && input[ct][0] != '')

    {

        ptstr[ct] = input[ct]; /* set ptrs to strings        */

        ct++;

    }

    stsrt(ptstr, ct);          /* string sorter              */

    puts(" Here's the sorted list: ");

    for (k = 0; k < ct; k++)

        puts(ptstr[k]) ;       /* sorted pointers            */

}

/* string-pointer-sorting function */

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] != ' ' && st[i] != '')

            i++;

        if (st[i] == ' ')

            st[i] = '';

        else // must have words[i] == ''

            while (getchar() != ' ')

                continue;

    }

    return ret_val;

}

#####################################################################################

/* mod_str.c -- modifies a string */

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define LIMIT 81

void ToUpper(char *);

int PunctCount(const char *);

void mod_str(void)

{

    char line[LIMIT];

    char * find;

   

    puts("Please enter a line:");

    fgets(line, LIMIT, stdin);

    find = strchr(line, ' ');   // look for newline

    if (find)                    // if the address is not NULL,

        *find = '';            // place a null character there

    ToUpper(line);

    puts(line);

    printf("That line has %d punctuation characters. ",

           PunctCount(line));

}

void ToUpper(char * str)

{

    while (*str)

    {

        *str = toupper(*str);

        str++;

    }

}

int PunctCount(const char * str)

{

    int ct = 0;

    while (*str)

    {

        if (ispunct(*str))

            ct++;

        str++;

    }

   

    return ct;

}

#####################################################################################

/* format.c -- format a string */

#include <stdio.h>

#define MAX 20

char * s_gets(char * st, int n);

void format(void)

{

    char first[MAX];

    char last[MAX];

    char formal[2 * MAX + 10];

    double prize;

   

    puts("Enter your first name:");

    s_gets(first, MAX);

    puts("Enter your last name:");

    s_gets(last, MAX);

    puts("Enter your prize money:");

    scanf("%lf", &prize);

    sprintf(formal, "%s, %-19s: $%6.2f ", last, first, prize);

    puts(formal);

}

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] != ' ' && st[i] != '')

            i++;

        if (st[i] == ' ')

            st[i] = '';

        else // must have words[i] == ''

            while (getchar() != ' ')

                continue;

    }

    return ret_val;

}

#####################################################################################

/* sprintf example */

#include <stdio.h>

int main ()

{

char buffer [50];

int n, a=5, b=3;

n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);

printf ("[%s] is a string %d chars long ",buffer,n);

return 0;

}

#####################################################################################

Explanation / Answer

CASE STUDY DEFINITIONS:

The program is about to analyze students name & roll number Information in the University listings.

ALGORITHMIC EXERCISES:

Line 1 : INPUT - Enter the students Information in the web portal;

Line 2: To enter the numbers of students name in list of pages;

Line 3: By using sorting string function methods to sort the list of names;

Line 4: By using modulation string function methods to uppercase names;

Line 5: By using format string function methods to combine list first & last names;

Line 6: Function 'Sprintf( )' is used to combine the last & first into array.

Line 7: OUTPUT - List of names is arrange by following last & first names.

Line 8: END CODE

-----------------------------------------------------------------------------------------------------------------------
PROGRAMMING LANGUAGE:

#include <stdio.h>

#include <string.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

#include<stdlib.h>

#include<ctype.h>

#define SIZE 81 /* string length limit, including */

#define LIM 20 /* maximum number of lines to be read */

#define MAX 20 /* maximum length to read */

#define HALT " "             /* null string to stop input          */

Void Main ( )

{

Int i,num,ct;

void stsrt(char *strings[ ], int num);       /* string-sort function */

char * s_gets(char * st, int n);

void sort_str(void)

{

char input[LIM][SIZE];            /* array to store input       */

char *ptstr[LIM];                     /* array of pointer variables */

int ct = 0;                                /* input count                */

int k;                                      /* output count               */

printf("Input up to %d lines, and I will sort them. ",LIM);

printf("To stop, press the Enter key at a line's start. ");

while (ct < LIM && s_gets(input[ct], SIZE) != NULL && input[ct][0] != '')

{

ptstr[ct] = input[ct];                 /* set ptrs to strings        */

ct++;

}

stsrt(ptstr, ct);                             /* string sorter              */

puts(" Here's the sorted list: ");

for (k = 0; k < ct; k++)

puts(ptstr[k]) ;                             /* sorted pointers            */

}

/* string-pointer-sorting function */

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] != ' ' && st[i] != '')

i++;

if (st[i] == ' ')

st[i] = '';

else                                       // must have words[i] == ''

while (getchar() != ' ')

continue;

}

return ret_val;

getch();

}

/* mod_str.c -- modifies a string */

void ToUpper(char *);

int PunctCount(const char *);

void mod_str(void)

{

char line[LIMIT];

char * find;

puts("Please enter a line:");

fgets(line, LIMIT, stdin);

find = strchr(line, ' ');               // look for newline

if (find)                                      // if the address is not NULL,

*find = ''; // place a null character there

ToUpper(line); // Upper case letter line   

puts(line);

printf("That line has %d punctuation characters. ",

PunctCount(line));

}

void ToUpper(char * str)

{

while (*str)

{

*str = toupper(*str);

str++;

}

}

int PunctCount(const char * str)

{

int ct = 0;

while (*str)

{

if (ispunct(*str))

ct++;

str++;

}

return ct;

}

/* format.c -- format a string */

void format(char *)

char * s_gets(char * st, int n);

void format(void)

{

Int rollnm, prize;

char first[MAX];

char last[MAX];

char formal[2 * MAX + 10];

double prize;

puts("Enter your first name:");

s_gets(first, MAX);

puts("Enter your last name:");

s_gets(last, MAX);

puts(("Enter your class roll number:");

scanf(“%If”,&rollnm);

puts("Enter your prize money:");

scanf("%lf", &prize);

sprintf(formal, "%s, %-19s: $%6.2f ", last, first, prize);

puts(formal);

getch();

}

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] != ' ' && st[i] != '')

i++;

if (st[i] == ' ')

st[i] = '';

else // must have words[i] == ''

while (getchar() != ' ')

continue;

}

return ret_val;

}

/* sprintf example */

int main ()

{

char buffer [50];

int n, a=5, b=3;

n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);

printf("[%s] is a string %d chars long ",buffer,n);

return 0;

getch();

}

}

}

----------------------------------------------------------------------------------------------------------------

OUTPUT : LIST OF THE NAMES WILL BE DISPLAYED