C program to sort a string

C Strings:
Write a C program to sort a string.
In this program we sort the string using bubble sort technique.
Bubble Sort is the simplest and easiest sorting technique. In this technique, the two successive items A[i] and A[i+1] are exchanged whenever A[i]>=A[i+1]. The larger values sink to the bottom of the array and hence it is called sinking sort. The end of each pass smaller values gradually “bubble” their way upward to the top(like air bubbles moving to surface of water) and hence called bubble sort.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
int main(){
    int i,j,n;
    char str[50],temp[50];
    printf("Enter a String:nn");
    scanf("%s",str);
    n=strlen(str);
    for(i=0;i<=n;i++)
    for(j=i+1;j<=n;j++){
        if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
        }
    }
    printf("The sorted string is:%sn",str);
    return 0;
}
Read more Similar C Programs
Sorting in C
C Strings
C Aptitude

You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply