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

C Program to sort the string, using shell sort technique.

C Program to sort the string, using shell sort technique. Shell sort is the one of the oldest sorting technique, quite well for all types of arrays in c. The shell sort is a “diminishing increment sort”, better known as a “comb sort” to the unwashed programming masses. The algorithm makes multiple passes through the list, and each time sorts a number of equally sized sets using the insertion sort . The size of the set to be sorted gets larger with each pass through the list, until the set consists of the entire list. This sets the insertion sort up for an almost-best case run each iteration with a complexity that approaches O(n) . Read more about C Programming Language .

/***********************************************************
* 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 <string.h>
#include <stdio.h>
#include <stdlib.h>
void shell_sort(char *chars, int c) {

register int i, j, space, k;
char x, a[5];

a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;

for(k=0; k < 5; k++) {
space = a[k];
for(i=space; i < c; ++i) {
x = chars[i];
for(j=i-space; (x < chars[j]) && (j >= 0); j=j-space)
chars[j+space] = chars[j];
chars[j+space] = x;
}
}
}

int main() {
char string[300];
printf("Enter a string:");
gets(string);
shell_sort(string, strlen(string));
printf("The sorted string is: %s.n", string);
return 0;
}
Read more Similar C Programs
Array In C

Sorting Techniques

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

(you can send this program to your friend using this button)

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

(c) www.c-program-example.com