Sort character of string in ascending or descending order

Sort character of string in ascending or descending order


Write a program to sort set of strings in ascending and descending order of their ... char

Ascending

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main ()
{
    int n , i ,j ;
    char name[100] , temp;
    scanf("%s", name);
    n = strlen(name);
    for (i=0 ; i<n ; i++)
    {
        for(j=i+1 ; j<n ;j++)
        {
            if(name[i] > name[j])
            {
            temp = name[i]    ;
            name[i] = name[j] ;
            name[j] = temp;
            }
        }
    }

printf("\n%s" , name);
getch() ;
return 0 ;    
}

Descending

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main ()
{
    int n , i ,j ;
    char name[100] , temp;
    scanf("%s", name);
    n = strlen(name);
    for (i=0 ; i<n ; i++)
    {
        for(j=i+1 ; j<n ;j++)
        {
            if(name[i] < name[j])
            {
            temp = name[i]    ;
            name[i] = name[j] ;
            name[j] = temp;
            }
        }
    }

printf("\n%s" , name);
getch() ;
return 0 ;    
}

Comments

Popular posts from this blog

Copy Constructor

Print pattern in c - explore new perspectives in pattern printing

C program to check if the supplied year is leap year or not.