C Program to concatenate two String Using strcat() function

 
 
 

Algorithm:

 
step 1:start
 
step 2:declare a,b as character array
 
step 3:input a,b 
 
step 4:perform strcat() operation
 
step 5:print the concatenated string
 
step 6:stop

 

 

 

Flowchart:

 


Code:

 



#include <stdio.h>
#include<string.h>
int main() {
    char a[100],b[100];
    printf("Enter Your Name:");
    gets(a);
    printf("Enter Your Surname:");
    gets(b);
    strcat(a,b);
    printf("Your full name is %s",a);
    
    
    return 0;
}

Sample Input and Output:

Enter Your Name:Pankaj                                                                                                                 
Enter Your Surname:Jaiswal
Your full name is PankajJaiswal            

Explanation of above program 
 
In above Program we are concatenating one string with another string using strcat() function

char *strcat(char *destination,char *source)

The full form of strcat()  function is string concatenation.It is one of the inbuilt string function which is provided by string.h header file in C,it helps to concatenate two string those who don't what is concatenation,it is nothing but combining one string with another,I known most of you still have the confuse for  the same,So Let's take an example to understand more clearly.

first_string="Good"

second_string="Morning"

strcat(first_string,second_string)

After passing above two string values to strcat() function the result would be GoodMorning and it will be stored in the destination argument i.e first_string(as per syntax of strcat()).We can simply say that the result of concatenation of first_string and second_string will be stored in first_string.

Now Let's Understand the code line by line which we have written above

For understanding more clearly about C Program to concatenate two String Using strcat() function we have took the example in which User will put his/her first and last name and our program will give him his/her FullName.

In the first line of code we are importing stdio.h header file which help us to use printf() and scanf().stdio.h is a acronym of standard input output header file.In the next line we are importing string.h header file which provide different string related functions like strcmp(),stringcpy(),strcat(),
puts(),gets() and many more.

After that we have defined main function in which we have written all the statement which are going to be executed,as we known the execution of c program starts from main() function only.

In the next line,we have declared two character array(string) namely a,b which we will be using to stored two string respectively.After that we have printed one statement "Enter Your First Name" which help the user to get to known that the application is waiting for your input.For taking user input as string or character array we have used gets() function.Once the user put his/her first name the program will ask for lastname(here also we are doing the same thing which we were doing for firstname),the moment user put his/her last name the concatenated result of two string will be printed,to achieve this we are concatenating two string using strcat() functiom which we have already seen above how it works.

In the next line of code we are printing the result of concatenation using printf() function.Here we have written printf("Your Full name is  %s",a).Here "Your Full name is" will be printed as it is and the value of  a character array in which the concatenated sting is stored will be printed.

In the last line of code we are simply returning return 0 which mean that the execution of program is normally executed.It is necessary to return the value if we have wrote the int return of any function,In our case it is main() which is int return type.

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎   


 

Previous Next

1 Comments

Previous Post Next Post