C 语言连接两个字符串

如何编写 C 语言程序在不使用 strcat 函数的情况下连接两个字符串?在此编程中,我们可以通过多种方式连接两个字符串。但我们将讨论使用 For 循环、While 循环、函数和指针进行字符串连接的四种方法。

C 语言连接两个字符串(不使用 strlcat())

此程序允许用户输入两个字符串值或字符数组。接下来,它将使用 For 循环 遍历该数组中的每个字符并连接它们(或串联它们)。请同时参考 strcat 函数。

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100], Str2[100];
  	int i, j;
 
  	printf("\n Please Enter the First String :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);

        // To iterate First String from Start to end  
  	for (i = 0; Str1[i]!='\0'; i++);

        // Concatenating Str2 into Str1  	
  	for (j = 0; Str2[j]!='\0'; j++, i++)
  	{
  		Str1[i] = Str2[j];
  	}
  	Str1[i] = '\0';

  	printf("\n After the Concatenate = %s", Str1);
  	
  	return 0;
}
 Please Enter the First String :  Hello

 Please Enter the Second :  World

 After the Concatenate = HelloWorld

使用 While 循环进行 C 语言字符串连接的程序

此字符串连接程序与上面相同,但这次我们使用的是 While 循环(只需将 C 语言 编程 中的 For 循环替换为 While 循环)。

/* using a while loop */
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100], Str2[100];
  	int i, j;
 
  	printf("\n Please Enter the First String :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second String :  ");
  	gets(Str2);
 
  	i = 0;
	while( Str1[i]!='\0')
	{
		i++;
	}
	
  	j = 0;
  	while( Str2[j]!='\0')
  	{
  		Str1[i] = Str2[j];
  		i++;
  		j++;
  	}
  	Str1[i] = '\0';

  	printf("\n String after the Concatenate = %s", Str1);
  	
  	return 0;
}
C program to Concatenate Two Strings without using strcat 2

使用函数连接两个字符串

此字符串连接程序与上面相同。但是,我们目前正在使用 函数 概念将逻辑与主程序分开。

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

void combine(char [], char []); 

int main()
{
  	char Str1[100], Str2[100];
 
  	printf("\n Please Enter the First :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);
  	
  	combine(Str1, Str2);

  	printf("\n After the Concat = %s", Str1);
  	
  	return 0;
}

void combine(char s1[], char s2[])
{
	int i, j;
	
	i = 0;
	while( s1[i]!='\0')
	{
		i++;
	}
	
  	j = 0;
  	while( s2[j]!='\0')
  	{
  		s1[i] = s2[j];
  		i++;
  		j++;
  	}
  	s1[i] = '\0';
}
 Please Enter the First : C programming

 Please Enter the Second :  Tutorial

 After the Concat = C programmingTutorial

使用指针进行 C 语言字符串连接的程序

此字符串连接程序与第二个示例相同,但这次我们使用了 指针 概念。

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

int main()
{
  	char Str1[100], Str2[100];
  	char *s1 = Str1;
	char *s2 = Str2;
 
  	printf("\n Please Enter the First :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);
  	
  	while(* ++s1);
  	
  	while(*(s1++) = (*s2++)); 
  	
  	printf("\n After the Concatenate = %s", Str1);
  	
  	return 0;
}
 Please Enter the First :  Write a 

 Please Enter the Second :  C Program

 After the Concatenate = Write a C Program

使用指针函数进行连接

此 C 语言 编程 的字符串连接与上面相同。但是,这次我们将指针传递给 函数 以将逻辑与主程序分开。

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

void concatenate(char *, char *); 

int main()
{
  	char Str1[100], Str2[100];
 
  	printf("\n Please Enter the First :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);
  	
  	concatenate(Str1, Str2);

  	printf("\n After the Concatenate = %s", Str1);
  	
  	return 0;
}

void concatenate(char *Str1, char *Str2)
{
	while(*Str1)
	{
		Str1++;
	}
	
	while(*Str2)
	{
		*Str1 = *Str2;
		*Str1++;
		*Str2++;
  	}
  	*Str1 = '\0';
}
 Please Enter the First :  Good

 Please Enter the Second :  Morning All

 After the Concatenate = GoodMorning All