Monday, 22 February 2016

Non lexicographical order sorting

/**
* Created by Vijay Yalasangimath on 23-02-2016.
* Non lexicographical order sorting
*/
#include<stdio.h>
#include<string.h>

/**
*  returns 0 when both strings are equal
*          + str1 < str2 (In provided order)
*          - str1 > str2 (In provided order)
*/
long int mySort(char *a , char *b){

  long int c1,c2;
 char * myLex = "qwertyuiopasdfghjklzxcvbnm";
 char *p1,*p2;
 p1=strchr(myLex, *a);
 p2=strchr(myLex, *b);

  c1=p1-myLex+1;
 c2=p2-myLex+1;

  while((a[0]!='\0'||b[0]!='\0')&&(c1==c2)){
     p1=strchr(myLex, *a);
     p2=strchr(myLex, *b);

     c1=p1-myLex+1;
     c2=p2-myLex+1;

      a++;
     b++;
 }

  return c2-c1;
}

int main(){
    char str1[25],str2[25];

    printf("String will be compared in following order: qwertyuiopasdfghjklzxcvbnm\n");

    printf("Enter the strings for comparison\n");
    printf("First string:");
    scanf("%s",str1);
    printf("Second string:");
    scanf("%s",str2);

    printf("%ld\n", mySort(str1,str2));
    return 0;
}