Dark mode logo
Last Updated:
Concatenate two strings without using library function in c

Concatenate two strings without using library functions in C

Algorithm

Input: Input two strings
output: Concatenate the two strings and displays it

  1. Start
  2. len = 0 , i = 0
  3. Input the first string say str1
  4. Input the second string say str2
  5. while ( str1[i] != '\0')
    5.1 len++
    5.2 i++
  6. i = 0
  7. while ( str2[i] != '\0')
    7.1 str1[len] = str2[i]
    7.2 len++
    7.3 i++
  8. str1[len] = '\0'
  9. Print "The concatenated string is", str1
  10. 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