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.

10 comments:

  1. nice..but not dealt with lexicographic type sorting

    ReplyDelete
  2. nice but try to use without built-in function

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

    ReplyDelete
    Replies
    1. use char str[20] instead of str[20][20]

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. #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();
    }

    ReplyDelete
  6. why you use str[20][20] and not str[20] only?

    ReplyDelete