C language | concatenate two strings without extract

Example 67: Write a program in C language to connect two strings, do not use the strcat function.

Problem-solving ideas: First, there must be two keyboard entries, to enter string 1 and string 2, and then to achieve splicing. When readers look at this example, they can first think about how to write code using the strcat function, and then you can view it Check the source code of strcat to see how the bottom layer is written.

Source code demo:

#include<stdio.h>//头文件 
int main()//主函数 
{
    
    
  char str1[80],str2[40];//定义字符数组 
  int i=0,j=0;//定义整型变量且赋初值 
  printf("输入字符串1:");//提示语句 
  scanf("%s",str1); //录入字符串1 
  printf("输入字符串2:");//提示语句 
  scanf("%s",str2); //录入字符串2 
  while(str1[i]!='\0')//判断str1是不是最后一个字符 
  {
    
     
    i++;
  }
  while(str2[j]!='\0')//判断str2是不是最后一个字符 
  {
    
    
    str1[i++]=str2[j++];//逐个拼接 
  }
  str1[i]='\0';
  printf("\n新的字符串是:%s\n",str1);//输出拼接后的字符串 
  return 0;//主函数返回值为0 
}

The compilation and running results are as follows:

输入字符串1:L
输入字符串2:ove

新的字符串是:Love

--------------------------------
Process exited after 3.903 seconds with return value 0
请按任意键继续. . .

Above, if you see it and think it is helpful to you, please give Xiaolin a thumbs up and share it with the people around him, so that Xiaolin will also have the motivation to update, thank you fathers and villagers~

C language splicing strings
More cases can go to the public account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/113064094