Write a C program that will read a word and rewrite it in alphabetical order
#clanguage #c++ Language #programing #Question #article
/* C program that will read a string and rewrite it in the alphabetical order. i.e. the word HELLO should be written as EHLLO. */
#include"stdio.h"
#include"string.h"
void main()
{
char str[20], k;
int i, j;
printf("Enter a string: \n");
scanf("%[^\n]", str);
for(i=0; str[i] != '\0'; i++)
{
for(j=i+1; str[j] != '\0'; j++)
{
if(str[i] > str[j])
{
k= str[i];
str[i] = str[j];
str[j] = k;
}
}
}
printf("%s", str);
printf("\n");
}
Below is example of sort a string in alphabetical order using C++
/* C++ Program to sort a string in alphabetical order */
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
sort(s.begin(), s.end());
for (auto c : s)
cout << c;
return 0;
}
C Program to arrange the string in alphabetical order through two different language.
first example using c and another is C++ .In C++ we are using sort method to sort a string .