C language | copy strings without strcpy

Example 69: Write a program in C language to copy all the characters in the character array str2 to the character array str1, without using the strcpy function.

Analysis: "\0" should also be copied when copying, and the characters after "\0" will not be copied.

Source code demo:

#include<stdio.h>//头文件 
#include<string.h>//引入求字符串长度的库函数 
int main()//主函数 
{
    
    
  char str1[80],str2[80];//定义字符数组 
  int i;//定义整型变量 
  printf("输入要复制的字符串:");//提示语句 
  scanf("%s",str2);//输入字符串 
  for(i=0;i<=strlen(str2);i++)//遍历 
  {
    
    
    str1[i]=str2[i];//挨个赋值 
  } 
  printf("复制后的字符串是:%s\n",str1);//输出复制后的 
  return 0;//主函数返回值为0 
}

The compilation and running results are as follows:

输入要复制的字符串:love
复制后的字符串是:love

--------------------------------
Process exited after 3.853 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 | Copy the string without strcpy.
More cases can go to the official account : C language entry to proficient

Guess you like

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