文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序习题-复制指定位置后面的字符串[10.7]

C程序习题-复制指定位置后面的字符串[10.7]

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

    有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。     我们可以通过函数传递两个字符串的指针,然后读取一个指定位置的字符,对其进行赋值判断赋值后字符串是否为'\0',不是'\0'则继续循环复制,直到出现'\0'为止。编写代码如下:  

#include <stdio.h>
#define N 300

void mystrcpy(char *, char *, int);
int mystrlen(char *);
int main(int argc, char *argv[])
{
    char ch1[N],ch2[N];
    char *p_ch1,*p_ch2;
    int idx,len;
    p_ch1 = ch1;
    p_ch2 = ch2;
    printf("please input source string:\n");
    gets(ch1);
    printf("please input the index:");
    scanf("%d",&idx);
    len = mystrlen(p_ch1);
    while (idx < 0 || idx > len )
    {
          printf("your input index value is error,\nthe index >= 0 and index < %d,please reinput index value:",len);
          scanf("%d",&idx);
    }
    mystrcpy(p_ch1,p_ch2,idx);
    printf("the new string is : '%s'\n",p_ch2);
    system("pause");
    return 0;
}

void mystrcpy(char *from, char *to,int idx)
{
     while (*to++ = from[idx++]);
}

int mystrlen(char *string)
{
    int i = 0;
    while (*string++)
    {
          i++;
    }
    return i;
}


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

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载