Append String at the end of given String
This operation is used to append a new string at the end of existing string.
In order to append new string first we have to find length of existing string. Once length of existing string is found we can start copying characters from new string into existing string untill NULL character is encountered in new string.
Algorithm to Append String at the end of given String
| Step 1: | Length1 = strlen(S1) Length2 = 0 |
| Step 2: | Repeat Step3 while S2[Length2] ≠ NULL |
| Step 3: | S1[Length1] = S2 [Length2] Length1 = Length1 + 1 Length2 = Length2 + 1 |
| Step 4: | S1[Length1] = NULL |
Program to Append String at the end of given String
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s1,*s2;
int p,n;
void sapp(char *s1,char *s2);
clrscr();
puts("Enter String:");
gets(s1);
puts("Enter String");
gets(s2);
sapp(s1,s2);
puts(s1);
getch();
}
void sapp(char *s1,char *s2)
{
int length1=strlen(s1);
int length2=0;
while(s2[length2]!='\0')
{
s1[length1]=s2[length2];
length1++;
length2++;
}
s1[length1]='\0';
}