data structure assignment2-setB-Q2:sort strings in ascending order

 Data structure using c:

2) Write a ‘C’ program to accept names from the user and sort in alphabetical order using bubble sort

answer:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define l 6

 

void display(char array[][30]){

  for(int i=0; i<l; i++){

    printf("%s ", array[i]);

  }

  printf("\n");

   printf("\n");

}

 

int main()

{

 

  char array[l][30];

 

  printf("Enter %d Strings: \n", l);

  for(int i=0; i<l; i++){

    scanf("%s", array[i]);

  }

 

 

  printf("Original array: ");

  display(array);

 

  char temp[30];

 

 

  for(int i=0; i<l; i++){

    for(int j=0; j<l-1-i; j++){

      if(strcmp(array[j], array[j+1]) > 0){

     

        strcpy(temp, array[j]);

        strcpy(array[j], array[j+1]);

        strcpy(array[j+1], temp);

      }

    }

  }

 

 

  printf("Sorted Array: ");

  display(array);

 

 

}


Comments

Popular posts from this blog

PHP ALL ASSIGNMENT PDF

DATA STRUCTURE ALL PDF(LAB ASSIGNMENTS)

DATA STRUCTURE :ASSIGNMENT NO.8:TREE