In: Computer Science
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language.
- Only use stdio.h
- Input prompt should say "Enter string of your choice: "
- Remove any newline \n from input string
- Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm
- Output should print sorted string on new line
Example:
Enter string of your choice: Vex'ahlia
'Vaaehilx
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<stdio.h>
void selectionSort(char* s){
int i, j;
char temp;
int min_index;
for(i=0; s[i]!='\0'; i++){
min_index = i;
for(j=i+1; s[j]!='\0'; j++){
if(s[min_index]>s[j]){
min_index = j;
}
}
temp = s[i];
s[i] = s[min_index];
s[min_index] = temp;
}
}
int main(){
char word[101];
printf("Enter string of your choice: ");
scanf("%s",word);
selectionSort(word);
printf("%s", word);
return 0;
}
=================================================================