n0va_sa

Lexicographical String Checker

Jun 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int lexCompare(char* a, char* b){
  5.     int i;
  6.     int r = strlen(a);
  7.     int q = strlen(b);
  8.  
  9.     // ret 1 for a is small &&& ret -1 for b is small
  10.  
  11.     for(i=0;(i<r||i<q);i++){
  12.  
  13.         if(a[i] == '\0') return 1;
  14.         else if(b[i] == '\0') return -1;
  15.  
  16.         else if(a[i] < b[i]){
  17.             return 1;
  18.         }
  19.         else if(a[i] > b[i]){
  20.             return -1;
  21.         }
  22.  
  23.     }
  24.     return 0;
  25. }
  26. int main() {
  27.     char str1[] = "Zeo", str2[] = "Zed";
  28.  
  29.     printf("The result is : %d",
  30.            lexCompare(str1,str2)
  31.     );
  32.  
  33.  
  34.     return 0;
  35. }
Add Comment
Please, Sign In to add comment