Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Arrays and Strings
  5. Strings
  6. String Concatenation

String Concatenation

strcat()

Append a string at the end of other

strcat(first_string,second_string);

Program: concatenating strings using string function

#include<stdio.h>
int main()
{
	 char s1[100];
	char s2[100];
	puts("Enter s1");
	gets(s1);
	puts("Enter s2");
	gets(s2);
	strcat(s1,s2);
	puts("After concatenation s1 contains");
	puts(s1);
	return 0;
}

Output

Enter s1
sjit
Enter s2
students
After concatenation s1 contains
Sjitstudents

Program: Concatenating strings without using string function

#include<stdio.h>
int main()
{
	 char s1[100];
	char s2[100];
	int i=0,j=0;
	puts("Enter s1");
	gets(s1);
	puts("Enter s2");
	gets(s2);
	while(s1[i] != '\0')
	{
		i++;
	}
	while(s2[j] !='\0')
	{
		s1[i] = s2[j];
		i++;
		j++;
	}
	s1[i] = '\0';
	puts("After concatenation s1 contains");
	puts(s1);
	return 0;
}

Output

Enter s1
sjit
Enter s2
students
After concatenation s1 contains
sjitstudents

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments