How to Concatenate Strings in C without using strcat?

String manipulation benefits developers because it enables them to handle and modify strings as demanded. One approach is to apply strcat ().

What is strcat in C?

As the cat indicates concatenation. Its characteristic is to add the strings passed as arguments to this procedure, defined in the string.h of the C and cstring.h of the C++ header files.

The strcat() feature of string.h header combined the strings str1 next to the str2 string. The original string is mirrored, and then a Null character is appended.

The character in the str1 string beginning substitutes the null character or the endpoint in the str2 string, forming a new concatenated string.

Also Read: How to Convert a String into Uppercase without using Strupr in C

Syntax of strcat in C

strcat(char *str2, char *str1);

The aforementioned parameter indicates the following:

str2: It holds a C string sufficient to accommodate the resulting text after concatenation. The string is to be extended.
str1: The string which will be inserted. This should not be in contradiction with the str2.

This function returns the string str2 that has been concatenated.

Also Read: How to Compare two Strings without using Strcmp in C?

C Program to Concatenate Two Strings using strcat Function.

In the following example, the strcat() method is used.

Code:

#include <stdio.h>

#include <string.h>

//mentioning a header that features strcat ().

char str2[15] = “Hello”;
char str1[15] = “ world”;
//construct temporary variables to hold a string array.

strcat(str2, str1);
//concatenate str1 string to str2.

printf(“%s”,str2);
//print the resulting strings contained in str2.

Result: Helloworld

How to Concatenate Strings in C without using strcat?

There are numerous methods for concatenating two strings without strcat, but the process stays the same.

Code:

Using function

void stringConcatenation (char* str1, char* str2)
{
int i;
//introducing a variable

int j = strlen(str1);

//strlen can be used to determine the length of a string.

for (i = 0; str2[i] != ‘\0’; i++) {

// The for loop will be continued until no characters are found in str2.

str1[i + j] = str2[i];

//to insert a character from string str2 at position str1[i+j]/.

} str1[i + j] = ‘\0’;

//include the null character to signify the string’s conclusion.

return;

}

int main()
{

char str1[12] = “ABC”;
str2[12] = “abc”;
//declaring the variable

stringConcatenation(str1, str2);
//string str1 and str2 are provided for concatenation are called.

printf(“Concatenated String is: ‘%s’\n”, str1);
//output needed string

return 0;
}

Output: abcABC

What is the Difference Between strcat and strcpy?

strcat() delivered the appended string in str2 from the string provided as an argument to the function, namely str2 and str1. As discussed above.

In contrast to the strcat(), strcpy() duplicates the given string into the given variable, i.e., var, in this case, is the variable where hello world will be copied.

Syntax:
strcat( char *str2, char *str1);
//return str2, which contains the string value that was combined.

strcpy(char var, “hello world”);
//the indicated string or variable is copied into the variable.

What is strncat vs strcat?

Its working is similar to that of strcat() with a condition. The strcat() concatenates its argument str1 to the str2 string ending but appends str1 to str2 up to the n characters and then terminates it by the null character, i.e., \0.

Syntax: strncat(char *str2, const char *str1, n);

Example:

char s[5] = “Good”;
char str2[5] = “Day India”;
//defining a temporary variable

strncat(str2, str1, 3);
//Add the last three characters of str1 to str2.

printf(“%s”,str2);
//print the outcome value

Output: GoodDay

Greetings, My name is Rumi Sadaf. I work as both a content writer and a programmer. In essence, I explain what I know and aid others in understanding programming concepts. Happy Viewing.

Leave a Comment