In-Class Practice

String Functions

Complete the following program.  Add code to

  1. prompt for and accept the names of a several students
  2. accept "^" as a signal to end the name input process
  3. display the names in sorted order

Note that the sort function provided below. 

 // String Functions
 #include <stdio.h>
 #include <string.h>
 #define MAX_STUDENTS 5
 #define MAX_LETTERS 30
 void bubble(char [][MAX_LETTERS+1], int);

 int main(void)
         int noStudents;
         char student[MAX_STUDENTS][MAX_LETTERS+1]; 

         return 0;

 void bubble(char a[][MAX_LETTERS+1], int size)
         int i, j;
         char temp[MAX_LETTERS+1];

         for (i = size - 1; i > 0; i--) {
                 for (j = 0; j < i; j++) {
                         if (strcmp(a[j],a[j+1]) > 0) {
                                 strcpy(temp, a[j]);
                                 strcpy(a[j], a[j+1]);
                                 strcpy(a[j+1], temp);

Compare your answers with those of one of your collegues and make sure that the two of you agree.

   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page

  Designed by Chris Szalwinski   Copying From This Site   

Creative Commons License