1

I want to compare numbers read from numberplates. Each element that I want to compare looks like "CCNNNN" where C is a character from A-Z and N is a digit from 0 to 9. For example: TS9548

It defines the following total order on such numbers. Consider two distinct numbers A=A1A2M1M2M3M4 and B=B1B2N1N2N3N4

We say A<B if either:

A1A2 < B1B2 in alphabetic order or, A1A2 = B1B2 and M1M2M3M4 < N1N2N3N4 as integers.

Examples:

  • TS5480 < WB1915

  • AP9540 < TS7480

  • KL1452 < KL1457

Input Format:

Each line of the input will give two distinct numbers and separated by a space.

Input read from text file:

TS45678 NM78902
HJ78654 JK78901
GH00000 DE55555

Each line ends with a '\n' character.

Output:

For each line read,

Output 1 if A < B followed by a '\n'

Output 0 if B > A followed by a '\n'

Output -1 if B = A followed by a '\n'

My Code:

#include<stdio.h>

int main(){

  char numberPlate[2][8];
 
  
  
  while (scanf("%s %s",&numberPlate[0], &numberPlate[1])!=EOF){
      
      printf(numberPlate[0]);
      printf("\n");
      printf(numberPlate[1]);
    
    for (int i=0;i<=7;i++)
        
        {
            if (numberPlate[0][i]>numberPlate[1][i])
            {  
        
            printf("%s is greater than %s\n",numberPlate[0], numberPlate[1]);
        
            }
            
            else
            printf("%s is lesser than %s\n",numberPlate[0], numberPlate[1]);
        
        break;
        }
        
        
  }
  return(0);
}

Output:

$ cat input.txt | ./final.exe
TS45678
NM78902TS45678 is greater than NM78902
HJ78654
JK78901HJ78654 is lesser than JK78901
GH00000
DE55555GH00000 is greater than DE55555

I am not able to read input as line by line which stores TS45678 in row 1 and NM78902 in row 2 of numberPlate[2][8] array.. Secondly, the comparison functions seems incorrect.

MAC
  • 1,345
  • 2
  • 30
  • 60
  • You can use [`strcmp()`](https://en.cppreference.com/w/c/string/byte/strcmp) on the two strings containing the license plate numbers, that will give you the correct ordering. – G. Sliepen Sep 26 '20 at 10:32
  • without using any inbuilt function. – MAC Sep 26 '20 at 10:33
  • also, I am not able to read string the way in which I want. – MAC Sep 26 '20 at 10:34
  • 1
    Why are you not allowed to use `strcmp()`? Is this an assignment? Also, `printf()` and `scanf()` are also functions from the standard library. Are you also not allowed to use those? – G. Sliepen Sep 26 '20 at 10:35
  • Not working. It's giving ouput as TS45678NM78902 NM78902 HJ78654JK78901 JK78901 GH00000DE55555 DE55555 – MAC Sep 26 '20 at 10:46
  • Not working. Can you test on your side. – MAC Sep 26 '20 at 10:49
  • while (scanf("%7s %7s",numberPlate[0], numberPlate[1]) == 2){ << this is also wrong >> – MAC Sep 26 '20 at 11:03
  • Does this answer your question? [How does strcmp() work?](https://stackoverflow.com/questions/12136329/how-does-strcmp-work) – Den-Jason Sep 26 '20 at 11:17
  • `scanf("%s"...` is no better than `gets()`. Use `scanf("%7s"...)` to read into a buffer of size 8. – William Pursell Sep 26 '20 at 11:22
  • 1
    This would be a better exercise if you used `qsort`. You'd probably learn more. – William Pursell Sep 26 '20 at 11:26

2 Answers2

2

The minimal changes to make your code working:

  • a printf("\n"); is missing

     printf(numberPlate[0]);
     printf("\n");
     printf(numberPlate[1]);
     printf("\n"); // missing
    
  • The for loop breaks when the plates are not equal but the case there are equal isn't handled

     while (/* ... */)
     {
         for (i=0;i<=7;i++)     
         {
             if (numberPlate[0][i]>numberPlate[1][i])
               {
                 printf("%s is greater than %s\n",numberPlate[0], numberPlate[1]);
                 break; 
               }
             if (numberPlate[0][i]<numberPlate[1][i])
               { 
                 printf("%s is lesser than %s\n",numberPlate[0], numberPlate[1]);
                 break;
               }        
         }
         // You should handle here the case they are equal.
         // [your code here]
      }
    

I let you figure out what you can do to check out they are equals.

These are the minimal changes. As said in the comment, this code isn't robust enough to handle bad inputs.

Amessihel
  • 5,891
  • 3
  • 16
  • 40
1

If you're not allowed to use strcmp, write your own:

int strCmp(const char *a, const char *b)
{
    while (*a != '\0' && *b != '\0' && *a++ == *b++)
        ;

    return *b-*a;
}

This will be enough to solve your problem.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75