
Concatenate two strings without using library functions in C
Algorithm
Input: Input two strings
output: Concatenate the two strings and displays it
- Start
- len = 0 , i = 0
- Input the first string say str1
- Input the second string say str2
- while ( str1[i] != '\0')
5.1 len++
5.2 i++ - i = 0
- while ( str2[i] != '\0')
7.1 str1[len] = str2[i]
7.2 len++
7.3 i++ - str1[len] = '\0'
- Print "The concatenated string is", str1
- Stop
C Program
#include<stdio.h>
#include<string.h>
int main()
{
int len=0;
char str1[30],str2[30];
printf("Enter the first string ending with $(max 10 characters):");
gets(str1);
printf("Enter the second string ending with $(max 10 characters):");
gets(str2);
for(int i=0; str1[i]!='\0'; i++) {
len++;
}
for(int i=0; str2[i]!='\0'; i++) {
str1[len]=str2[i];
len++;
}
str1[len]='\0';
printf("The concatenated string is ");
puts(str1);
return 0;
}
Output
Enter the first string ending with $(max 10 characters):Hello$
Enter the second string ending with $(max 10 characters):World$
The concatenated string is Hello$World$
Comments