Download presentation
Presentation is loading. Please wait.
Published bySharmila Patil Modified over 5 years ago
3
C program to concatenate two strings without using library function
In this program, we will learn how to concatenate or add two strings without using library function? Here we are implementing a function like strcat() that will add two strings. The function stringCat() will be used in this program to concatenate two strings, where strings will be read by the user. Program to concatenate/add two strings without using library function in C
4
#include <stdio. h> #include <string
#include <stdio.h> #include <string.h> #define MAX_SIZE 100 /******************************************************** * function name :stringCat * Parameter :char* s1,char* s2 * Return :void * Description :Concatenate two strings
5
/ void stringCat (char. s1,char
********************************************************/ void stringCat (char *s1,char *s2); int main() { char str1[MAX_SIZE],str2[MAX_SIZE]; printf("Enter string 1 : "); scanf("%[^\n]s",str1);//read string with spaces getchar();//read enter after entering first string printf("Enter string 2 : "); scanf("%[^\n]s",str2);//read string with spaces stringCat(str1,str2); printf("\nAfter concatenate strings are :\n"); printf("String 1: %s \nString 2: %s",str1,str2); printf("\n"); return 0; } /******** function definition
6
/ void stringCat (char. s1,char
*******/ void stringCat (char *s1,char *s2) { int len,i; len=strlen(s1)+strlen(s2); if(len>MAX_SIZE) { printf("\nCan not Concatenate !!!"); return; } len=strlen(s1); for(i=0;i< strlen(s2); i++) { s1[len+i]=s2[i]; } s1[len+i]='\0'; /* terminates by NULL*/ } Output Enter string 1 : Hello Enter string 2 : Friends After concatenate strings are : String 1: HelloFriends String 2: Friends
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.