Question

In: Computer Science

PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem...

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.

Solutions

Expert Solution

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::


Related Solutions

PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
USE C++ for the program and kindly keep it as simple as possible , use recursion...
USE C++ for the program and kindly keep it as simple as possible , use recursion and do not use loops please keep program simple and comment if possible Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The program will now be broken up into methods and store all the inventory in an ArrayList object. The program will be able to run a report for all inventory details as well as a report for items that are low in...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The Food and Book items should inherit all the properties of the Product item in the previous assignment. Foods cannot be added to the inventory without an expiration date. Implement a toString method for Product, Food, and Book. Grading details: Correct usage...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking integers until the user enters -100. In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered. Sample Input/Output 1: Input the number: 0 2 3 -9 -6 -4 -100 Number of positive numbers: 2 Number of Negative numbers: 3 Number of Zero: 1
Write the simple shell in C language. Please show some outputs.
Write the simple shell in C language. Please show some outputs.
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program simple. Q (5) Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely inserted_text (of type string), characters_count (of type unsigned int), words_count...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT