Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2509517
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-04 17:27:27

    编写一程序,将两个字符串连接起来,不要用strcat函数。
    我们可以回忆一下如何才能将两个字符串连接起来。肯定是我们要先找到第一个字符串的末尾即'\0'处,然后将第二个字符串的每一个字符都copy到第一个字符串中。但是在c语言中字符串都是以'\0'作为结束,因此,我们还需要手动的赋值最后一个字符串为'\0'.即可实现函数strcat的功能。根据上面的思路,编写代码如下:

#include <stdio.h>
#define N 100

int main(int argc, int *argv[])
{
    char str1[N];
    char str2[N];
    char c;
    int i = 0,j = 0;
    printf("please input s1 string:");
    gets(str1);
    printf("please input s2 string:");
    gets(str2);
    printf("strcat result:");

    for (i = 0; str1[i] != '\0'; i++)
    {
        ;
    }
    for (j = 0; (c = str2[j]) != '\0'; j++)
    {
        str1[i++] = c;
    }
    str1[i] = '\0';
    
    puts(str1);
    system("pause");
    return 0;
}


阅读(2059) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~