SORTING OF STRING USING C PROGRAM






Program for sorting of string in c language

#include<stdio.h>
int main(){
  int i,j,n;
  char str[20][20],temp[20];
  puts("Enter the no. of string to be sorted");
  scanf("%d",&n);
  for(i=0;i<=n;i++)
      gets(str[i]);
  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\n");
  for(i=0;i<=n;i++)
      puts(str[i]);
  return 0;
}





3. Write a c program to delete the all consonants from given string.

16 comments:

Unknown said...

nice..but not dealt with lexicographic type sorting

Karpov said...

nice but try to use without built-in function

Anonymous said...

In this program type mismatch error occur.....whats the reason...how to prevent it..??

vishwas.liet@gmail.com said...
This comment has been removed by the author.
vishwas.liet@gmail.com said...

#include"string.h"
#include"stdio.h"
#include"conio.h"
void main()
{
clrscr();
//char string1[],string2[];
char string1[]= "Goodhell0";
char string2[]= "Morning";
char string3[17];


//merge two strings
strcat(string3,string1);
strcat(string3 ,string2);
printf("%s",string3);

//sort two string
char min[1],next[1];
int loc=0;
int len = strlen(string3);
//printf("%d",len);
//int minchar , nextchar;
for(int i=0;i < len;i++)
{
loc = i;
min[0] =(char)string3[loc];
for(int j=i;j < len;j++)
{
next[0]=(char)string3[j];
if(int(min[0]) > int(next[0]))
{
min[0] =next[0];
loc = j;
}
}
string3[loc] = string3[i];
string3[i] = min[0];
//printf("\n %s", min);
}
printf("\n %s",string3);
getch();
}

Unknown said...

put #include

Anonymous said...

good

Anonymous said...

use char str[20] instead of str[20][20]

Anonymous said...

why you use str[20][20] and not str[20] only?

Unknown said...

GOOD APPROACH...PLZZ....TO TAKE A INPUT..AND SHOW THE OUT PUT ALSO....WHICH CLEAR THE ALL SCENARIO....

Anmol said...

Nice one

Unknown said...

put #include

Unknown said...

string

Unknown said...

put #include

shery said...

what to declare temp ? an int or char ? because both definitions are showing error

EEEan said...

I think it's not clear... approach