C Program:
#include<stdio.h> #include<string.h> #include<conio.h> void main() {
int i,j,n; char str[20][20],temp[20]; puts("\nEnter the number of strings to be sorted\n"); scanf("%d",&n); printf("\nEnter the strings one by one\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) /* Comparison line */ { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } } printf("\nThe sorted strings:\n"); for(i=0;i<=n;i++) puts(str[i]); getch(); }
C++ program:
#include<iostream.h> #include<string.h> #include<stdio.h> #include<conio.h> void main() { int i,j,n; char str[20][20],temp[20]; cout<<"\nEnter the number of strings to be sorted\n"; cin>>n; cout<<"\nEnter the strings one by one\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) /* Comparison line */ { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } } cout<<"\nThe sorted strings:\n"; for(i=0;i<n;i++) puts(str[i]); getch(); }
No comments :
Post a Comment