Tuesday 24 June 2014

C++ program to sort the characters in a given string


Here is a C++ program to sort the characters in a given string


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],temp;
    int lenght;
    cout<<"Enter String : ";
    cin>>str;
    lenght=strlen(str)-1;
    for(int i=0;i<lenght;i++)
    {
        for(int j=0;j<lenght;j++)
        {
            if(str[j]>str[j+1])
            {
                temp=str[j];
                str[j]=str[j+1];
                str[j+1]=temp;
            }
        }
    }
    cout<<"Sorted string = "<<str;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment