In: Computer Science
PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT
In this problem you will take a list of names from the user and sort them alphabetically using selection sort.
The code for selection sort for integer is available in the "Lab and Weekly Coding Solution module" in webcourses.
Ask the user how many names the user wants to input. Let's say the number be N.
Then take N number of names and put them in an array of strings.
Then write a function and name it as selection_sort. This function takes an array of strings and sort it alphabetically. The sorting should not be case sensitive.
Then in the main function print the strings in sorted order. After sorting, your code should prompt for a string to search. Based on the input, your program should find all the names containing the given search string as a sub string.
Sample Input/Output (italic texts are part of input)
Enter how many names: 5
Name 1: Torben Pedersen
Name 2: Nusair Ahmed
Name 3: Mikael
Name 4: nasir ahmed
Name 5: Mikel
Sorted Names:
mikael
mikel
nasir ahmed
nusair ahmed
torben pedersen
Enter search string: el
mikael
mikel
Hints:
In order to solve the String problem 1 in repl.it, you can use the selection sort code you have written or the one I have uploaded in weekly coding solutions module.
The concept is very similar to an integer sorting. However, in the case of string, you have to use strcmp function and strcpy function for comparing and swapping strings.
For finding searching sub string, you can use strstr() function of string.h library
Rubric:
Total point 3
Taking input properly: 0.25
Converting string to lower case properly: 0.25
comparing string properly: 0.5 point
Swapping properly: 0.5 point
Performing the sorting properly and call: 1
Finding sub string properly: 0.5
Not passing each test case is -1.
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 100
void selectionSort(char arr[][MAX_LEN], int n)
{
int i, j, min_idx;
int k=0;
for(i =0;i<n;i++)
{
int len = strlen(arr[i]);
for(j=0;j<len;j++)
{
arr[i][j]= tolower(arr[i][j]);
}
}
char minStr[MAX_LEN];
for (i = 0; i < n-1; i++)
{
int min_idx = i;
strcpy(minStr, arr[i]);
for (j = i+1; j < n; j++)
{
// If min is greater than arr[j]
if (strcmp(minStr, arr[j]) > 0)
{
// Make arr[j] as minStr and update min_idx
strcpy(minStr, arr[j]);
min_idx = j;
}
}
// Swap the found minimum element
if (min_idx != i)
{
char temp[MAX_LEN];
strcpy(temp, arr[i]); //swap item[pos] and item[i]
strcpy(arr[i], arr[min_idx]);
strcpy(arr[min_idx], temp);
}
}
}
int main()
{
char arr[20][MAX_LEN];
int n;
char searchStr[MAX_LEN];
printf("Enter how many names: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("\nName %d: ",i+1);
scanf(" %[^\n]s",&arr[i]);
}
selectionSort(arr, n);
printf("\nSorted names:\n");
for (int i = 0; i < n; i++)
printf("%s\n\n", arr[i]);
printf("\n\nEnter search string: ");
scanf("%s",searchStr);
printf("\n\n");
for(int i=0;i<n;i++)
{
char* ret;
ret = strstr(arr[i], searchStr);
if(ret)
{
printf("%s\n\n",arr[i]);
}
}
return 0;
}
SCEENSHOTS::