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

‘strncpy( s2, s1,n)’ is a standard library function which copies only first n characters from the source s1 to target s2 excluding the null character ‘\0’.

 

Example:

We are taking the input from the user using command line arguments.
input = $ ./a.out Subhash programming 4
output = prog

In the given example “Subhash” is the target string and “programming “is the source string . So argv[1] = “Subhash ” , argv[2] = “programming” and argv[3] = n .The strncpy( ) copies first 4 characters of of argv[2] into the argv[1] . Therefore in this case the output string is “prog”.

 

Logic:

1.Take source , target strings and ‘n’ using command line arguments.
2.Use malloc and allocate memory for the given input.
3.Now the pointer (p) created is will be pointing to the allocated memory.
4.Call the function my_strncpy( ) and pass the base address of the argv[1] and argv[2].( my_strncpy(p, argv[2],n ) .
5.The control goes to the function defination( void my_strncpy(char * s1, char * s2).
6.Now consider a variable ‘i’ and check if the the pointer s1 has reached ‘\0’ and (i < n) condition(while(*s2 != ‘\0’ && i < n)).
7.If the above condition is true start copying each character of the value to which s1 is pointing (*s2) into the corresponding character of the value to which s2 is pointing (*s2) and increment ‘i’ .
8.If the codition specified in 6 fails then append the ‘\0’ character and print the string s1.

 

Source code:

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void my_strncpy(char* , char*,int);
int main(int argc,char* argv[])
{
        int size,i,n;
        n = atoi(argv[3]);
        size = strlen(argv[2]) + 1;
        char* p = malloc(size);
        char* q = p;
        for(i = 0; argv[1][i] != '\0';i++)
       {
                 *q++ = argv[1][i];
      }
      *q = '\0';
       my_strncpy(p,argv[2],n);
       printf("copied string is %s\n",p);
}
void my_strncpy(char* s1 ,char* s2,int n)
{
          int i = 0;
          while(*s2 != '\0' && i < n)
         {
                 *s1++ = *s2++;
                  i++;
         }
         *s1 = '\0';
}

 

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.