Implementing ‘strstr()’ 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 strstr( s1,s2):

strstr( s1, s2) is a standard library function which finds whether the string s2 is a substring of s1 or not. This function return address of the begining of the substring or NULL if the substring is not found.

 

Example:

 

We are taking the input from the user using command line arguments.
1. input = $ ./a.out Subhash bhash
output =  bhash
2. input = $ ./a.out Subhash bath
output = NULL

In case “Subhash” is the string and “bhash” is the substring. So s argv[1] = “Subhash” and argv[2] = “bhash”. Therefore strstr( ) will check wherher the argv[2] is a substring of argv[1] or not ,in this case it will return the address of the character s1[2].

 

Logic:

 

1. Take source and target strings using command line arguments.
2. Call the function my_strstr( ) and pass the base address of the argv[1] and argv[2].( my_strstr(argv[1] , argv[2]) ).
3. The control goes to the function defination( int my_strstr(char * p, char * q).
4. Check if s1 has encounter ‘\0’.
5.If the above condition is false then compare present character of s1 with first character of s2 .
6 .If the above condition(4) is true make the flag = 1,indicating the possibility of s2 to be present in s1.
7.check if s2 has reached ‘\0’.
8.If not then compare present character of s1 is equal to the present character of s2.If the condition fails then make flag = 0 and come out of the loop.
9.If condition specified in 5 fails then check the value of flag.If 1 then comeout of the loop, if flag = 0 then continue the loop.
10.After above conditions are checked ,check the value of flag, if 1 then return address of s1[i] else return NULL.

Source code:

 

#include<stdio.h>
char * my_strstr(char *,char *);
int main(int argc, char * argv[])
{
          int i = 0;
          char *str = my_strstr(argv[1], argv[2]);
          printf("%p %s",str,str);
}
char *my_strstr(char *s1,char *s2)
{
         int i,j,k,flag = 0;
        for(i = 0; *(s1+i)!= '\0';i++)
      {
                 if(*(s1+i) == *(s2+0))
                 {
                            flag = 1;
                           for(j = 0,k = i;*(s2+j)!= '\0';j++,k++)
                           {
                                             if(*(s1+k) != *(s2+j))
                                             {
                                                         flag = 0;
                                                         break;
                                              }
                             }
               }
              if(flag)
                     break;
        }
        if(flag)
                return (s1 +i);
         else
                return NULL;
}

 

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

No Comment

You can post first response comment.

Leave A Comment

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