文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序习题-不使用strcpy进行字符串copy[7.15]

C程序习题-不使用strcpy进行字符串copy[7.15]

时间:2010-08-05  来源:chengxiaopeng

    编写一个程序,将字符数组s2中的全部字符拷贝到字符数组s1中,不用strcpy函数。拷贝时,'\0'也要copy过去,'\0’后面的字符不拷贝。     我们知道在字符串函数中有一个是strlen函数,这个函数是计算字符数组的长度。但是此函数的字符长度不包括最后的'\0'值。因此当我们从第零位开始copy,直到copy到i小于等于strlen,这样即可将最后的结束字符'\0'copy过去,根据此原理,代码编写如下:  

#include <stdio.h>
#define N 100

int main(int argc, int *argv[])
{
    char str1[N],str2[N];
    char c;
    int i;
    printf("please inputa s2 string:");
    gets(str2);
    
    for (i = 0 ; i <= strlen(str2) ; i++)
    {
        str1[i] = str2[i];
    }
     
    printf("print s1 string:");
    puts(str1);
    
    system("pause");
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载