Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS101 / KCS201 Programming for Problem Solving - Using C Language


Lab Exercises


28. Write a program to implement strlen ( ), strcat ( ), strcpy ( ) using the concept of Functions.


/*
 File: Prgrm28.c
 Author: Aditya Saini
 Date: Sept 30, 2019
 Description: Program to implement strlen ( ), strcat ( ) and strcpy ( ).
*/

#include <stdio.h>

int my_strlen (char *);
char * my_strcat (char *, char *);
char * my_strcpy (char *, char *);

int main (void)
{
               char string_1 [20], string_2 [20];
               int length;

               //Input strings.
               printf ("Input first string: ");
               gets (string_1);
               printf ("Input second string: ");
               gets (string_2);

               //Calling function my_strlen.
               length = my_strlen (string_1);
               printf ("\nLength of \"%s\": %d\n", string_1, length);
               length = my_strlen (string_2);
               printf ("Length of \"%s\": %d\n", string_2, length);

               //Calling function my_strcat.
               my_strcat (string_1, string_2);
               printf ("\n***After concatenation***\n");
               printf ("First string: \"%s\"\n", string_1);
               printf ("Second string: \"%s\" ", string_2);

               //Calling function my_strcpy.
               my_strcpy (string_2, string_1);

               printf ("\n\n***After coping first string into second string***\n");
               printf ("First string: \"%s\"\n", string_1);
               printf ("Second string: \"%s\" ", string_2);

               return 0;
};

int my_strlen (char *string)
{
               int count = 0;

               //Finding length of string.
               while (string [count] != '\0')
               {
                              count++;
               }

               return count;
};

char * my_strcat (char *string_1, char *string_2)
{
               int i, j;

               //finding the last element of string
               i = 0;
               while (string_1 [i] != '\0')
                              i++;

               //Concatenating second string
               j = 0;
               while (string_2 [j] != '\0')
               {
                              string_1 [i + j] = string_2 [j];
                              j++;
               }
               string_1 [i + j] = '\0';

               return string_1;
};

char * my_strcpy (char *string_1, char *string_2)
{
               int i;

               //Coping string_1 into string_2
               i = 0;
               while (string_2 [i] != '\0')
               {
                              string_1 [i] = string_2 [i];
                              i++;
               }
               string_1 [i] = '\0';

               return string_1;
};

Output

Input first string: www.digital
Input second string: g1.com

Length of "www.digital": 11
Length of "g1.com": 6

***After concatenation***
First string: "www.digitalg1.com"
Second string: "g1.com"

***After coping first string into second string***
First string: "www.digitalg1.com"
Second string: "www.digitalg1.com"



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib