Old answer below


You can initialize a string with strcpy, like in your code, or directly when declaring the char array.

char a1[100] = "Vivek";

Other than that, you can do it char-by-char

a1[0] = 'V';
a1[1] = 'i';
// ...
a1[4] = 'k';
a1[5] = '\0';

Or you can write a few lines of code that replace strcpy and make them a function or use directly in your main function.


Old answer

You have

        0 1 2 3 4 5 6 7 8 9 ...
    a1 [V|i|v|e|k|0|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_]
    b1 [R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_|_|_|_|_|_]

and you want

        0 1 2 3 4 5 6 7 8 9 ...
    a1 [V|i|v|e|k|R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_]

so ...

a1[5] = 'R';
a1[6] = 'a';
// ...
a1[12] = 'l';
a1[13] = '\0';

but with loops and stuff, right? :D

Try this (remember to add missing bits)

for (aindex = 5; aindex < 14; aindex++) {
    a1[aindex] = b1[aindex - 5];
}

Now think about the 5 and 14 in the loop above.

What can you replace them with? When you answer this, you have solved the programming problem you have :)

Answer from pmg on Stack Overflow
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ concatenate-two-strings-without-using-strcat
C Program to Concatenate Two Strings Without Using strcat
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.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ concatenate-string
C Program to Concatenate Two Strings | Programiz
July 3, 2020 - #include <stdio.h> int main() { char s1[100] = "programming ", s2[] = "is awesome"; int length, j; // store length of s1 in the length variable length = 0; while (s1[length] != '\0') { ++length; } // concatenate s2 to s1 for (j = 0; s2[j] != '\0'; ++j, ++length) { s1[length] = s2[j]; } // terminating the s1 string s1[length] = '\0'; printf("After concatenation: "); puts(s1); return 0; } ... Here, two strings s1 and s2 and concatenated and the result is stored in s1.
People also ask

How can I concatenate two strings in C?
You can concatenate two strings in C using various methods: Using loops to copy characters manually. Using the strcat() function from . Using pointers for direct memory manipulation. Using dynamic memory allocation to handle larger or unknown string sizes.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
What is the difference between strcat() and strncat() for concatenating two strings?
strcat() joins entire strings, while strncat() allows you to append only a limited number of characters from the second string.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
What is string concatenation in C programming?
String concatenation in C programming refers to combining two strings into one by appending the characters of one string to the end of another, ensuring the result is properly null-terminated.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2015 โ€บ 02 โ€บ c-program-to-concatenate-two-strings-without-using-strcat
C Program to concatenate two strings without using strcat
In the following program user would be asked to enter two strings and then the program would concatenate them. For concatenation we have not used the standard library function strcat(), instead we have written a logic to append the second string at the end of first string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-concatenate-two-strings-without-using-strcat
C Program to Concatenate Two Strings Without Using strcat - GeeksforGeeks
July 23, 2025 - In this method, we use pointer arithmetic to traverse and concatenate the two strings. First, move the pointer to the end of the first string, then copy each character from second string one by one.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-to-concatenate-two-strings
C program to Concatenate Two Strings without using strcat
April 2, 2025 - This program allows users to enter two string values or character arrays. Next, it will use For Loop to iterate each character in that array and joins them (or concatenate them). Please refer strcat function as well.
Top answer
1 of 7
4

Old answer below


You can initialize a string with strcpy, like in your code, or directly when declaring the char array.

char a1[100] = "Vivek";

Other than that, you can do it char-by-char

a1[0] = 'V';
a1[1] = 'i';
// ...
a1[4] = 'k';
a1[5] = '\0';

Or you can write a few lines of code that replace strcpy and make them a function or use directly in your main function.


Old answer

You have

        0 1 2 3 4 5 6 7 8 9 ...
    a1 [V|i|v|e|k|0|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_]
    b1 [R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_|_|_|_|_|_]

and you want

        0 1 2 3 4 5 6 7 8 9 ...
    a1 [V|i|v|e|k|R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_]

so ...

a1[5] = 'R';
a1[6] = 'a';
// ...
a1[12] = 'l';
a1[13] = '\0';

but with loops and stuff, right? :D

Try this (remember to add missing bits)

for (aindex = 5; aindex < 14; aindex++) {
    a1[aindex] = b1[aindex - 5];
}

Now think about the 5 and 14 in the loop above.

What can you replace them with? When you answer this, you have solved the programming problem you have :)

2 of 7
4
 char a1[] = "Vivek";

Will create a char array a1 of size 6. You are trying to stuff it with more characters than it can hold.

If you want to be able to accommodate concatenation "Vivek" and "Ratnavel" you need to have a char array of size atleast 14 (5 + 8 + 1).

In your modified program you are doing:

char *a1=(char*)malloc(100);  // 1
a1 = "Vivek";                 // 2

1: Will allocate a memory chunk of size 100 bytes, makes a1 point to it.
2: Will make a1 point to the string literal "Vivek". This string literal cannot be modified.

To fix this use strcpy to copy the string into the allocated memory:

char *a1=(char*)malloc(100);  
strcpy(a1,"Vivek");

Also the for loop condition i<strlen(b1)-1 will not copy last character from the string, change it to i<strlen(b1)

And

a1[i]='\0';

should be

a1[i + len]='\0';

as the new length of a1 is i+len and you need to have the NUL character at that index.

And don't forget to free your dynamically allocated memory once you are done using it.

Top answer
1 of 5
3

I didn't really change much in your program, but here's what I have (modified from yours) and it works.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
  {
    char s1[100], s2[100];
    size_t i, s1l, s2l, total;

    puts("First string?");
    fgets(s1, sizeof(s1), stdin);
    s1[strlen(s1) - 1] = 0x00;

    puts("Second string?");
    fgets(s2, sizeof(s2), stdin);
    s2[strlen(s2) - 1] = 0x00;

    s1l = strlen(s1);
    s2l = strlen(s2);
    total = s1l + s2l;

    for(i = s1l; i <= total; i++)
      {
        s1[i] = s2[i - s1l];
      }

    puts(s1);

    return(0);
  }

Using your program verbatim, the problem was that since the length of s1 was changing at each iteration of the loop, the strlen(s1) in the check kept increasing in value so you would basically end up with a mostly infinite loop...at least until it encountered some random null byte. However, when taking the length of the strings before hand, the loop terminus count does not change which results in the proper output. My code will compile and run correctly on a FreeBSD 10.2 system using the clang compiler.

As a side note, DO NOT return void on main. ALWAYS return int. Also specify the library headers that you plan to use.

EDIT: I modified the code to use fgets instead of gets since gets is completely unsafe. It does not check buffer sizes so you can easily have a buffer overflow.

2 of 5
1

The length of s1 is getting changed at runtime, making your index i incorrect. Try something like below:-

l = strlen(s1);

for(i = 0; i < strlen(s2); i++)
{
   s1[l++] = s2[i];
}

s1[l] = '\0';

Suppose s1 = "hello" and s2 = "world", then in the first iteration length of s1 = 5 and index for s2 = 0(i- length(s1)); works well. But in the second iteration length(s1) = 6 and index for s2 = 0(i-length(s1)). so index for taking the character from s2 is not changing. This was about the problem in your implementation, though you should an efficient way by using sprintf.

sprintf(s1, "%s%s", s1, s2);
Find elsewhere
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
September 17, 2026 - Learn 5 different ways to concatenate two strings in C with examples and step-by-step explanations. Master string manipulation techniques efficiently!
๐ŸŒ
Log2Base2
log2base2.com โ€บ c-examples โ€บ string โ€บ concatenate-two-strings.html
C program to concatenate two strings without using strcat function
#include<stdio.h> int main() { char str1[100],str2[100],str[200]; int length = 0,i; scanf("%s%s",str1,str2); /* Let's assume * first string as "abc", * second string as "def" */ /* fill the first string str1 * str[0] = 'a' length = 1 * str[1] = 'b' length = 2 * str[2] = 'c' length = 3 */ for(i = 0; str1[i] != '\0'; i++) str[length++] = str1[i]; /* fill the second string str2 * str[3] = 'd' length = 4 * str[4] = 'e' length = 5 * str[5] = 'f' length = 6 */ for(i = 0; str2[i] != '\0'; i++) str[length++] = str2[i]; /*append null char '\0' at the end of string *str[6] = '\0'; */ str[length] = '\0'; /* * str[0] = 'a' * str[1] = 'b' * str[2] = 'c' * str[3] = 'd' * str[4] = 'e' * str[5] = 'f' * str[6] = '\0' */ printf("Final String = %s",str); return 0; } Run it
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to concatenate two strings
C program to concatenate two strings - Codeforwin
July 20, 2025 - As I already talked many times in my previous string posts. In C programming NULL is represented using 0. Hence, the above two NULL checking conditions in while loop is unnecessary and can be stripped. /** * C program to concatenate two strings */ #include <stdio.h> #define MAX_SIZE 100 // Maximum string size int main() { char str1[MAX_SIZE], str2[MAX_SIZE]; int i, j; /* Input two strings from user */ printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); /* Move till the end of str1 */ i=-1; while(str1[++i]); /* Copy str2 to str1 */ j = 0; while(str1[i++] = str2[j++]); printf("Concatenated string = %s", str1); return 0; }
๐ŸŒ
Programming9
programming9.com โ€บ programs โ€บ c-programs โ€บ 130-c-program-for-string-concatenation-with-out-using-strcat
C Program for String Concatenation without using strcat()
#include <string.h> #include <stdio.h> int main() { int i,len=0,j; char str[50],str1[50],ch,ch1; printf("\nEnter the First String : " ); gets(str); printf("\nEnter the Second String : " ); gets(str1); for(i=0; str[i]!='\0'; i++) { len++; } for(i=0; str1[i]!='\0'; i++) { str[len++]=str1[i]; } str[len]='\0'; printf("\n Concatenated String is: %s ",str); return 0; }
Top answer
1 of 3
3

In main you declared character arrays with three elements

char string1[3] = "abc";
//...
char string2[3] = "def";

These arrays do not contain strings because they do not store the terminating zero character '\0' of the string literals used as initializers. The first array contains three characters { 'a', 'b', 'c' } and the second array contains these three characters { 'd', 'e', 'f' }.

Then you applied the standard string function strlen to these arrays which expects that passed to it arrays contains strings. That is the function calculates the number of characters in a string by counting characters until the terminating zero character is encountered. As the arrays do not contain the terminating zero character '\0' that is as the arrays do not contain strings the calls of strlen invoke undefined behavior.

If you want to append one string to another string then the destination character array shall have enough size to be able to accommodate the second string.

Thus the function main can look the following way

int main( void ) {
    char string1[7] = "abc";
    char string2[] = "def";

    printf( "Resultant string = %s\n", strconcatenate( string1, string2 ) );

    return 0;
}

Now the both arrays string1 and string2 contain strings. Moreover the array string1 reserved enough space to accommodate the string stored in the array string2.

The declaration of the function strconcatenate should be similar to the declaration of the standard C function strcat.

char * strconcatenate( char *string1, const char *string2 );

That is the second parameter should have the qualifier const because the passed array is not changed within the function and the function should return the destination array that will contain the concatenated strings.

The function definition will look the following way

char * strconcatenate( char *string1, const char *string2 ) 
{
    for ( char *p = string1 + strlen( string1 ); *p++ = *string2++; );

    return string1;
}
2 of 3
2

There are multiple problems in your code:

  • [major] char string1[3] = "abc"; defines the destination array with a size of 3 bytes which does not have a enough space for the string "abc" including its null terminator and definitely not long enough to receive the extra characters from string2 at the end. Change this to char string1[7] = "abc";
  • [major] char string2[3] = "def"; defines the source array with a size of 3 bytes which does not have a enough space for the string "def" including its null terminator, hence will not be null terminated.
  • [major] string1[strlen(string1) - 1] = '\0'; overwrites the last character of abc, this not necessary.
  • [major] same remark for string2[strlen(string2) - 1] = '\0';
  • [minor] string2 should be defined as const char * in void strconcatenate(char *string1, char *string2)
  • [minor] i and j should be defined with type size_t
  • [minor] for compatibility with the standard function strcat, strconcatenate should have a return type of char * and return the pointer to the destination array. Yet, As commented by Jonathan Leffler, a more useful design for the return value is to return a pointer to the null at the end of the concatenated string. You already know where the start of the string is; that isn't very interesting information. But knowing where the end of the string is after concatenation โ€” that is useful information which only the concatenate function is privy to. โ€“

Here is a modified version:

#include <stdio.h>
#include <string.h>

char *strconcatenate(char *string1, const char *string2) {
    char *p = string1 + strlen(string1);
    while ((*p = *string1) != '\0') {
        string1++;
        p++;
    }
    return string1;  /* strcat semantics */
    //return p;      /* alternative semantics */
}

int main() {
    char string1[7] = "abc";
    char string2[] = "def";

    strconcatenate(string1, string2);
    printf("Resultant string = %s\n", string1);
    return 0;
}
๐ŸŒ
Filo
askfilo.com โ€บ higher education โ€บ smart solutions โ€บ write a c program to concatenate two strings without using bui
Write a C Program to concatenate two strings without using built-in funct..
December 29, 2025 - #include <stdio.h> int main() { char str1[100], str2[50]; int i, j; // Input first string printf("Enter the first string: "); fgets(str1, sizeof(str1), stdin); // Remove newline character from fgets if present for(i = 0; str1[i] != '\0'; i++) { if(str1[i] == '\n') { str1[i] = '\0'; break; } } // Input second string printf("Enter the second string: "); fgets(str2, sizeof(str2), stdin); // Remove newline character from fgets if present for(j = 0; str2[j] != '\0'; j++) { if(str2[j] == '\n') { str2[j] = '\0'; break; } } // Find the end of the first string for(i = 0; str1[i] != '\0'; i++); // Append second string to first string for(j = 0; str2[j] != '\0'; j++, i++) { str1[i] = str2[j]; } // Null terminate the concatenated string str1[i] = '\0'; // Output the concatenated string printf("Concatenated string: %s\n", str1); return 0; }
๐ŸŒ
myCompiler
mycompiler.io โ€บ view โ€บ 3N01Z9PButM
To concatenate two strings without using strcat (C) - myCompiler
March 3, 2024 - #include <stdio.h> #include <string.h> int main() { int len1,len2; int i; char name1[20]="Tanishka"; char name2[20]="Kumar"; len1=strlen(name1); len2=strlen(name2); for(i=0;i<=len2;i=i+1) { name1[len1+i]=name2[i];} printf("%s",name1); }
Published: Mar 03, 2024
๐ŸŒ
Onlinegdb
learn.onlinegdb.com โ€บ c_program_to_concatenate_two_strings_without_using_strcat
C Program to concatenate two strings without using strcat() | Learn Programming step by step
Problem Statement: Write a C Program to concatenate two strings without using strcat() Required Knowledge: C Input/Output, C Variables, C Datatypes, C for..loop ยท Solution: Explanation: Tags ยท C Examples ยท C Intermediate Examples ยท
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ strings concatenation in c
Strings Concatenation in C - Scaler Topics
April 11, 2024 - In it, values taken by the user are stored in string_1 and string_2 and a resultant string is taken. First, the string_1 is inserted into the resultant string and then the string_2 is inserted into the resultant string. Let us see an example. ... Explanation: In the above example, we can see ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-a-C++-program-to-concatenate-two-strings-without-using-the-strcat-function
How to write a C++ program to concatenate two strings without using the strcat function - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.