C program to arrange characters of a word in alphabetical order.
#include <stdio.h>
#include <string.h>
int main() {
char input[256], ch;
int i, j;
/* get the input string from the user */
printf("Enter your input string:");
fgets(input, 256, stdin);
input[strlen(input) - 1] = '\0';
/* sort the characters in the given string */
for (i = 0; i < strlen(input) - 1; i++) {
ch = input[i];
for (j = i + 1; j < strlen(input); j++) {
if (ch > input[j]) {
ch = input[j];
input[j] = input[i];
input[i] = ch;
}
}
}
/* print the resultant string */
printf("Sorted letters in a string:%s\n", input);
return 0;
}
#include <string.h>
int main() {
char input[256], ch;
int i, j;
/* get the input string from the user */
printf("Enter your input string:");
fgets(input, 256, stdin);
input[strlen(input) - 1] = '\0';
/* sort the characters in the given string */
for (i = 0; i < strlen(input) - 1; i++) {
ch = input[i];
for (j = i + 1; j < strlen(input); j++) {
if (ch > input[j]) {
ch = input[j];
input[j] = input[i];
input[i] = ch;
}
}
}
/* print the resultant string */
printf("Sorted letters in a string:%s\n", input);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string: HelloWorld
Sorted letters in a string: HWdellloor
Enter your input string: HelloWorld
Sorted letters in a string: HWdellloor
No comments:
Post a Comment