Created
September 15, 2016 21:17
-
-
Save priyadarshitathagat/3b62ff318597690b38ca81d1c201b258 to your computer and use it in GitHub Desktop.
Program to sort strings in c using pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
//to enter number of strings and then sort the string lexicographically | |
main() | |
{ | |
char str[10][20],*p=&str[0][0]; | |
printf("Enter n\n"); int i,n; | |
scanf("%d",&n); | |
for(i=0; i<n; i++) | |
scanf("%s",(p+i*20)+0); | |
sort(&str,n); | |
for(i=0; i<n; i++) | |
printf("%s\n",(p+i*20)+0); | |
} | |
sort(char *p,int n) | |
{ | |
int i=0,j; | |
for(; i<n-1; i++) | |
{ for(j=i; j<n; j++) | |
if(strcmp((p+i*20),(p+j*20))>0) | |
{ char ch[20],*q=&ch[0]; | |
strcpy(q,(p+i*20)); | |
strcpy((p+i*20),(p+j*20)); | |
strcpy((p+j*20),q); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment