Implementing ‘strcmp()’ using Command Line Arguments

IMPLEMENTATION OF STANDARD STRING LIBRARY FUNCTIONS USING COMMAND LINE ARGUMENTS

Command line arguments:

In C language main() is the only function which can defined in multiple ways.It can recieve no arguments, two arguments or 3 arguments. Using these arguments the main function will recieve the arguments from the console.
The main function can be define in the following ways:
1. int main( )
2. int main( int argc, char *argv[ ])

argc:
It is called as argument count ,it tells the number of arguments that is being passed to the main function.

argv[ ]:
This is called as argument vector ,its an array whose each element would be pointing to the arguments that are passed to the main function.

Implementing ‘strcmp(s2,s1)’:

strcmp( s2, s1) is a standard library function which compares the source s1 with the target string s2.

If both the strings are equal the it returns a value ‘0’.

If the source string is greater than the target string it, returns a positive value .

If the source string is smaller than the target string ,it returns a negetive value .

Example:

we are taking the input from the user using command line arguments.
1.input = $ ./a.out  AAHAa  AAHA
result = 97 (i.e. the difference between ascii value of ‘a’ and ‘\0’).
2.input = $ ./a.out  AAHA  AAHA
result = 0 ( Both the strings are equal).
3.input = $ ./a.out AAHA  AAHAA
result = -65 (i.e the difference between ascii value of ‘\0’ and ‘A’).

In case of the first input given above “AAHAa” is the source string and “AaHA”is the target string .
So argv[1] = “AAHAa”, argv[2] = “AAHA”. So strcmp( ) compares the each character of argv[1] with corresponding individual character of argv[2] and when the corresponding invidual characters are not same then it returns the difference of the ASCII values of those characters, in this case it returns positive value ie 97 ( difference between ‘a’ and ‘\0’).

Logic:

1. Take source and target strings using command line arguments.
2.Call the function my_strcmp(argv[1] , argv[2] ) and pass the base address of the argv[1] and argv[2].
3.The control goes to the function defination( int my_strcmp(char * p, char * q).
4.check if each character of the value to which p is pointing (*p) is equal to the corresponding character of the value to which q is pointing( while(*p = = *q)).
5.If the above condition is true check if the pointer p has reached ‘\0’ or not.If yes return ‘0’ to the main program else increment both the pointers and continue the loop.
6.If the condition specified in 4 fails then return the difference between the ASCII characters.

 

Source code:

#include<stdio.h>
 int my_strcmp(char *,char *);
 int main(int argc, char*argv[])
 {
           int r = my_strcmp(argv[1] , argv[2]);
          printf("%d",r);
 }
 int my_strcmp(char *p,char *q)
 {
            for( ;*p == *q; p++, q++)
            {
                      if(*p == '\0')
                     {
                                 return 0;
                      }
            }
        return (*p - *q);
 }

 

This article is written by one of my student as part of their blog writing exercise.

To learn more programs on C and C++, click this link: Interview Questions

Subhash.K.U, Principal Mentor, Subhash Embedded Classes,Bangalore.

VIEW SUBHASH’s PROFILE

SUBHASH LOVES TO TEACH ! CLICK HERE !

Subhash Embedded Classes is one of the Best Embedded Systems Training Institute in Bangalore

 

2 Response Comments

  • Mithun Malige  July 28, 2016 at 10:24 am

    In for loop the condition should be *p == *q.

    Reply
    • subhash_user  July 28, 2016 at 11:10 am

      Corrected ! Thanks Mithun ! :)

      Reply

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.