- 0
Notifications Mark All Read
- Login
- Get Prime
Program to Concatenate Two Strings
Concatenate Two Strings :
Concatenate is defined as addition of two characters while concatenation of two string is merging of two strings in to one single string.
Working of Program :
In the program, we will take two string of different lengths and concatenate by using algorithm.
Run
#include<stdio.h> int main() { char str1[100] = "PrepInsta "; char str2[100] = "is one of the best for preparing placement"; // finding length of string 1 int length =0; while(str1[length] != '\0'){ length++; } for(int i=0; str2[i] != '\0'; i++){ str1[length] = str2[i]; length++; } str1[length] = '\0'; puts(str1); return 0; }
Output :
PrepInsta is one of the best for preparing placement
In the above program,
- We take two string of different length.
- First we find the length of string str1 by using while loop.
- By using for loop , we add the particular character of string 2 in string 1 character by character
- puts will print the output on the screen
Example :
Let’s make problem more complex and concatenate three strings in to one string.
Run
#include<stdio.h> int main() { char str1[100] = "PrepInsta "; char str2[100] = "is one of the best for preparing placement "; char str3[100] = " and for techincal skill"; // finding length of string 1 int length =0; while(str1[length] != '\0'){ length++; } // adding second string to first string for(int i=0; str2[i] != '\0'; i++){ str1[length] = str2[i]; length++; } // adding third string into first string for(int i=0; str3[i] != '0'; i++){ str1[length] = str3[i]; length++; } str1[length] = '\0'; puts(str1); return 0; }
Output :
PrepInsta is one of the best for preparing placement and for techincal skill
What Happened Above?
In the above program, we take three different string and concatenate them in to one string using for loop.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment