1. Write a program in C that reads a list of student names and sorts the list. Y
ID: 3686000 • Letter: 1
Question
1. Write a program in C that reads a list of student names and sorts the list. Your program should have a function called “readNames” that has two arguments: 1) a 2-D character array, 2) an integer indicating the number of student names to be read from the keyboard. Another function called “sortNames” should have the same arguments and will reorder the list using a sorting algorithm (similar to the one we implemented for integer arrays) and puts names in ascending order. Assume that names are not longer than 10 characters including the nul character (‘’). Also, maximum number of names that will be entered by the user is 10.
Note 1: The 2-D character array serves as a way to store names. Each row of this array will save a single name (assuming we are only reading first names and they don’t include any spaces). For example if the user enters 3 names: Alex, Jack, and Rebekah, the 2D array would look like this:
Note 2: You can define the character array as:
char names[10][10];
At the end, write a main function that utilizes these two functions to read N names (main function should read N from the keyboard) and print them in ascending order.
2. Implement the following functions to operate on character strings. For this exercise, you are not allowed to include <string.h>. All of the character manipulation done by your functions must be done using the arrays passed into the functions as arguments. You may not define any additional arrays in your functions or globally.
For all of these descriptions, the arguments s, s1, s2 are character strings. The functions, reverse() and insert(), alter their first argument. The insert function does not check for overflow of the array of the first argument.
void reverse (char s[]) ;
void insert (char s1[], char s2[], int n);
bool search(char s1[], char s2[]);
Write a main function that reads two strings and shows the output of each of these three functions. Please, do not hard-code any string and let the user enter strings.
Explanation / Answer
1).
#include<stdio.h>
#include<string.h>
// function prototype
void readNames(char [][10], int);
void sortNames(char [][10], int);
int main(){
char names[10][10]; // declaring 2-D array
int n;
printf("Enter number of students(max 10): ");
scanf("%d", &n);
//calling readNames function
readNames(names, n);
//calling sortNames function
sortNames(names, n);
//printing
int i;
for(i=0; i<n; i++)
printf("%s ", names[i]);
return 0;
}
void readNames(char names[][10], int n){
int i;
for(i=0; i<n; i++){
printf("Enter name of %d student: ",(i+1));
scanf("%s", names[i]);
}
}
void sortNames(char names[][10], int n){
// appling selection sort
char temp[10];
int i, j, min_index;
for(i=0; i<n; i++){
min_index = i;
for(j=i+1; j<n; j++){
if(strcmp(names[min_index], names[j]) > 0)
min_index = j;
}
//swapping
strcpy(temp ,names[i]);
strcpy(names[i], names[min_index]);
strcpy(names[min_index], temp);
}
}
/*
sample output:
Enter number of students(max 10): 5
Enter name of 1 student: pfeff
Enter name of 2 student: bfeff
Enter name of 3 student: jfff
Enter name of 4 student: afef3f
Enter name of 5 student: defqf3
afef3f
bfeff
defqf3
jfff
pfeff
*/
2).
#include<stdio.h>
void reverse (char s[]) ;
void insert (char s1[], char s2[], int n);
int search(char s1[], char s2[]);
int main(){
char s1[40];
char s2[20];
printf("Enter first string: ");
scanf("%s", s1);
printf("Enter second string: ");
scanf("%s", s2);
reverse(s1);
printf("Reverse of first: %s ",s1);
//getting back s1
reverse(s1);
insert(s1, s2,7);
printf("Insert of first and second in first: %s ",s1);
//finding s2 in s1
int i = search(s1, s2);
if(i==1)
printf("s2 is in s1 ");
else
printf("s2 is not in s1 ");
return 0;
}
void reverse (char s[]){
//finding length
int len =0;
while(s[len] != '')
len++;
len = len-1;
int i =0;
// reverse
while(i < len){
char temp = s[i];
s[i] = s[len];
s[len] = temp;
i++;
len--;
}
}
void insert (char s1[], char s2[], int n){
//finding length of s1
int len =0;
while(s1[len] != '')
len++;
len = len-1;
//finding length of s2
//finding length of s1
int len2 =0;
while(s2[len2] != '')
len2++;
int i =0;
// reverse
while(i < n && i<len2){
s1[len + i] = s2[i];
i++;
}
s1[len+i] = '';
}
int search(char s1[], char s2[]){
int i = 0;
while(s1[i] != ''){
int j = 0;
while(s2[j] != ''){
if(s2[j] != s1[i+j])
break;
j++;
}
if(s2[j] == ''){
return 1; // found s2 in s1
}
i++;
}
//reach here, means do not find
return 0;
}
/*
sample run:
Enter first string: pravesh
Enter second string: kumar
Reverse of first: hsevarp
Insert of first and second in first: praveskumar
s2 is in s1
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.