What I have so far: I just need to implement the bubble sort algorithm in C Exer
ID: 3861453 • Letter: W
Question
What I have so far: I just need to implement the bubble sort algorithm in C
Exercise For the first version, you will implement the primary data structure a a 2-dimensional array StringsINUMIDLEN, where NUM is the number of strings, and LEN is the maximum length allowed for the string: the first index i gives you the i-th string, and the second index j gives you the j-th character within that string Using the starter file ex1.c, mplement bubble sort of strings. Your code must strictly follow these specifications: Use fgets() to read each string from the input, one string per line. Use LEN as an argument to fgets0 to ensure that an input line that is too long does not exceed the bounds imposed by the string's length. Note that the newline and NULL characters will be included in LEN, so the maximum number of printable characters in the string will actually be LEN-2 The comparison of two strings must be done by checking them one character at a time, without Using any C string library functions. That is, write your own loop to do this The swap of two strings must be done by copying them (using a temp variable of your choice) one character at a time, without using any C string library functions. That is, write your own loop to do this You are allowed to use C library functions for printing strings, e.g., fputs0, puts0, printf0, etc. You are allowed to use the C library function strlen0 to calculate the length of a string Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided. Refer to the sample inputs and outputs provided on exactly how to format your l/OExplanation / Answer
ex1.c :
____
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define NUM 25
#define LEN 1000
int main()
{
char Strings[NUM][LEN];
int x,i,j,k,z;
clrscr();
printf("Please enter %d strings, one per line: ",NUM);
for(z=0;z<NUM;z++){
fgets(Strings[z],LEN-2,stdin);
}
puts(" Here are the strings in the order you entered:");
for(x=0;x<NUM;x++){
printf("%s",Strings[x]);
}
/*Bubble sort*/
for(i=0;i<NUM;i++){
for(j=0;j<NUM-1;j++){
int flag = 0;
for(k=0;k<strlen(Strings[j]);k++){
if(Strings[j][k] > Strings[j+1][k]){
flag = 1;
break;
}
else if (Strings[j][k] == Strings[j+1][k])
continue;
else
break;
}
if(flag == 1){
char temp[LEN];
for(k=0;k<strlen(Strings[j+1]);k++){
temp[k] = Strings[j+1][k];
}
for(k=0;k<strlen(Strings[j]);k++){
Strings[j+1][k] = Strings[j][k];
}
for(k=0;k<strlen(temp);k++){
Strings[j][k] = temp[k];
}
}
}
}
/*Output sorted list */
puts(" In alphabetical order, the strings are:");
for(x=0;x<NUM;x++){
printf("%s",Strings[x]);
}
getch();
return 0;
}
Sample Input and Output:
______________________
Please enter 25 strings, one per line:
lakshman
mahesh
manoj
sravan kumar
kanakala
anakapalli
banana
dileep
pavan
naveen
vamsi
venky
sandeep sena
padma teja
sukesh
mohan
raju
bhardawaz
jackson
jane
venkatesh
surya
nooka raju
yogesh
zumla
Here are the strings in the order you entered:
lakshman
mahesh
manoj
sravan kumar
kanakala
anakapalli
banana
dileep
pavan
naveen
vamsi
venky
sandeep sena
padma teja
sukesh
mohan
raju
bhardawaz
jackson
jane
venkatesh
surya
nooka raju
yogesh
zumla
In alphabetical order, the strings are:
anakapalli
banana
bhardawaz
dileep
jackson
jane
kanakala
lakshman
mahesh
manoj
mohan
naveen
nooka raju
padma teja
pavan
raju
sandeep sena
sravan kumar
sukesh
surya
vamsi
venky
venkatesh
yogesh
zumla
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.