Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created September 15, 2016 21:17
Show Gist options
  • Save priyadarshitathagat/3b62ff318597690b38ca81d1c201b258 to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/3b62ff318597690b38ca81d1c201b258 to your computer and use it in GitHub Desktop.
Program to sort strings in c using pointers
#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