c program to concatenate two strings without using library functions
Answers
Answer:
#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
/* This loop is to store the length of str1 in i
* It just counts the number of characters in str1
* You can also use strlen instead of this.
*/
for(i=0; str1[i]!='\0'; ++i);
/* This loop would concatenate the string str2 at
* the end of str1
*/
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
// \0 represents end of string
str1[i]='\0';
printf("\nOutput: %s",str1);
return 0;
}
Answer:
Step 1. input
let two string variable s1 and s2 having input
s1 = “Ankit”;
s2 = “Singh”;
Step 2. Found Last position
Then found length of first string
= strlen(s1);
= strlen(“Ankit”);
= 5
Last position of s1 = 5
STEP 3. Concatenat
Now we can Concatenat second string using for loop
s1[5] = “S”
s1[6] = “i”
s1[7] = “n”
s1[8] = “g”
s1[9] = “h”