Create program that sorts words of a string based on first letter of each word. Use C programming language.
- Only use stdio.h and string.h
- Remove any newline \n from input string
- Use insert function
- Input prompt should say "Enter string of your choice: "
- Output should print sorted string on new line
Example:
Enter string of your choice: this is a string
a is string this
Code:
#include<stdio.h>
#include<string.h>
#define MAX 100
int main(){
int i,j,count=0;
char input[MAX],str[MAX][MAX],temp[MAX];
printf("Enter String: ");
scanf("%[^\n]%*c", input);
j=0;
for(i=0;input[i]!='\0';i++) {
if(input[i]!=' ') {
str[count][j++] = input[i];
} else {
count++;
j=0;
}
}
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("Sorted Strings: ");
for(i=0;i<=count;i++)
printf("%s ",str[i]);
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.