C Program to Find Total of Even Integers

C Program to Concatenation of Strings

The C program Concatenation of Strings is given below example.

The names of employees of an organization are stored in three arrays, namely first_name, second_name, and last_name. Write a program to concatenate the three parts into one string to be called name.

Hope to assign Concatenation of Strings?

Three for loops are used to copy the three strings. In the first loop, the characters contained in the first_name are copied into the variable name until the null character is reached. The null character is not copied; instead it is replaced by a space by the assignment statement

name[i] = \ / ;

Similarly, the second_name is copied into name, starting from the column just after the space created by the above statement. This is achieved by the assignment statement

name[i+j+1] = second_name[j];


If first_name contains 4 characters, then the value of i at this point will be 4 and therefore the first character from second_name will be placed in the fifth cell of name. Note that we have stored space in the fourth cell.

In the same way, the statement is used to copy the characters from last_name into the proper locations of name.

name[i+j+k+2] = last_name[k];

At the end, we place a null character to terminate the concatenated string name. In this example, it is important to note the use of the expressions i+j+1 and i+j+k+2.

Example: Concatenation of Strings

#include <stdio.h>
#include <string.h>
main()
{
   int i, j, k ;
   char first_name[10] = {"YASO"} ;
   char second_name[10] = {"TECHNOSAP"} ;
   char last_name[10] = {"INDIA"} ;
   char name[30] ;
/* Copy first_name into name */
   for( i = 0 ; first_name[i] != '
#include <stdio.h>
#include <string.h>
main()
{
int i, j, k ;
char first_name[10] = {"YASO"} ;
char second_name[10] = {"TECHNOSAP"} ;
char last_name[10] = {"INDIA"} ;
char name[30] ;
/* Copy first_name into name */
for( i = 0 ; first_name[i] != '\0' ; i++ )
name[i] = first_name[i] ;
/* End first_name with a space */
name[i] = ' ' ;
/* Copy second_name into name */
for( j = 0 ; second_name[j] != '\0' ; j++ )
name[i+j+1] = second_name[j] ;
/* End second_name with a space */
name[i+j+1] = ' ' ;
/* Copy last_name into name */
for( k = 0 ; last_name[k] != '\0'; k++ )
name[i+j+k+2] = last_name[k] ;
/* End name with a null character */
name[i+j+k+2] = '\0' ;
printf("\n\n") ;
printf("%s\n", name) ;
}
' ; i++ ) name[i] = first_name[i] ; /* End first_name with a space */ name[i] = ' ' ; /* Copy second_name into name */ for( j = 0 ; second_name[j] != '
#include <stdio.h>
#include <string.h>
main()
{
int i, j, k ;
char first_name[10] = {"YASO"} ;
char second_name[10] = {"TECHNOSAP"} ;
char last_name[10] = {"INDIA"} ;
char name[30] ;
/* Copy first_name into name */
for( i = 0 ; first_name[i] != '\0' ; i++ )
name[i] = first_name[i] ;
/* End first_name with a space */
name[i] = ' ' ;
/* Copy second_name into name */
for( j = 0 ; second_name[j] != '\0' ; j++ )
name[i+j+1] = second_name[j] ;
/* End second_name with a space */
name[i+j+1] = ' ' ;
/* Copy last_name into name */
for( k = 0 ; last_name[k] != '\0'; k++ )
name[i+j+k+2] = last_name[k] ;
/* End name with a null character */
name[i+j+k+2] = '\0' ;
printf("\n\n") ;
printf("%s\n", name) ;
}
' ; j++ ) name[i+j+1] = second_name[j] ; /* End second_name with a space */ name[i+j+1] = ' ' ; /* Copy last_name into name */ for( k = 0 ; last_name[k] != '
#include <stdio.h>
#include <string.h>
main()
{
int i, j, k ;
char first_name[10] = {"YASO"} ;
char second_name[10] = {"TECHNOSAP"} ;
char last_name[10] = {"INDIA"} ;
char name[30] ;
/* Copy first_name into name */
for( i = 0 ; first_name[i] != '\0' ; i++ )
name[i] = first_name[i] ;
/* End first_name with a space */
name[i] = ' ' ;
/* Copy second_name into name */
for( j = 0 ; second_name[j] != '\0' ; j++ )
name[i+j+1] = second_name[j] ;
/* End second_name with a space */
name[i+j+1] = ' ' ;
/* Copy last_name into name */
for( k = 0 ; last_name[k] != '\0'; k++ )
name[i+j+k+2] = last_name[k] ;
/* End name with a null character */
name[i+j+k+2] = '\0' ;
printf("\n\n") ;
printf("%s\n", name) ;
}
'; k++ ) name[i+j+k+2] = last_name[k] ; /* End name with a null character */ name[i+j+k+2] = '
#include <stdio.h>
#include <string.h>
main()
{
int i, j, k ;
char first_name[10] = {"YASO"} ;
char second_name[10] = {"TECHNOSAP"} ;
char last_name[10] = {"INDIA"} ;
char name[30] ;
/* Copy first_name into name */
for( i = 0 ; first_name[i] != '\0' ; i++ )
name[i] = first_name[i] ;
/* End first_name with a space */
name[i] = ' ' ;
/* Copy second_name into name */
for( j = 0 ; second_name[j] != '\0' ; j++ )
name[i+j+1] = second_name[j] ;
/* End second_name with a space */
name[i+j+1] = ' ' ;
/* Copy last_name into name */
for( k = 0 ; last_name[k] != '\0'; k++ )
name[i+j+k+2] = last_name[k] ;
/* End name with a null character */
name[i+j+k+2] = '\0' ;
printf("\n\n") ;
printf("%s\n", name) ;
}
' ; printf("\n\n") ; printf("%s\n", name) ; }

Output

YASO TECHNOSAP INDIA

Let me know if you find any difficulty in understanding this C Program to Concatenation of Strings example and I would be glad to explain it further.

Also Read :  C Program to Reading Of Strings with Example

Online Training Tutorials