C Program to Concatenate Two Strings Without Using strcat

C Programming Examples
C String Programs
C Loop Programs
C Function Programs
C Array Programs
C Integer Number Programs
C File I/O Programs

In C programming strcat() function is used to Concatenate strings but in this program we have concatenate string without using this function.

Program to Concatenate Strings without using strcat()


void main(void)
  char str1[25],str2[25];
  int i=0,j=0;
  printf("\nEnter First String:");
  printf("\nEnter Second String:");
  printf("\nConcatenated String is %s",str1);

This program is used to concatenate strings without using strcat() function. So first of all, you have to include the stdio header file using the "include" preceding # which tells that the header file needs to be process before compilation, hence named preprocessor directive.

Then you have to define the main() function and it has been declared nothing so by default it is integer heche returns an integer. The inside the main() function, you have to declare two character array name str1 and str2 having size 25 for both. Then you have o declare two integer variables j and j and initialized them with value 0. Then the printf() is used to display a message "Enter First String:". Followed by printf() you have to use the gets() to take the 1st string from the user. Similarly, another printf() is used to display the message "Enter Second String:" and using gets() takes the value of str2 from the user.

Now a while loop is implemented which checks whether str1[i] does not equal to '\0' or not, and increments the value of i by 1. Another while is used after this which checks whether str2[i] not equals to '\0', if the condition becomes true, the loop will get executed and where it is given the value of str2[j] gets initialized to str1[i]; Then increments the value of i and j by 1. And finally, initializes the str1[i] as '\0' i.e. null character terminator. At last printf() is used to display the value of the concatenated string.

Scroll Back to Top