Write a program to read a string through keyboard and sort it using bubble sort

Write a program to read a string through keyboard and sort it using bubble sort.
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int main(){
  5.    int i, j, n;
  6.    char str[25][25], temp[25];
  7.    printf("Enter the number of string : ");
  8.    scanf("%d", &n);
  9.  
  10.    printf("\nEnter %d strings : \n", n);
  11.    for(i=0; i<=n; i++)
  12.       gets(str[i]);
  13.      
  14.    for(i=0; i<=n; i++)
  15.       for(j=i+1; j<=n; j++) {
  16.          if(strcmp(str[i], str[j])>0) {
  17.             strcpy(temp, str[i]);
  18.             strcpy(str[i], str[j]);
  19.             strcpy(str[j], temp);
  20.          }
  21.       }
  22.    printf("\n\nSorted Strings :");
  23.    for(i=0; i<=n; i++)
  24.       puts(str[i]);
  25.   
  26.    return 0;
  27. }

একটি মন্তব্য পোস্ট করুন