人生理想:少劳多得 2022-11-25 10:18 采纳率: 94.7%
浏览 6
已结题

c语言有关指针的练习

指针类型练习:
将传入的字符串str1中从第index个字符开始的全部字符复制到字符串str2中。

注意: index 必须小于 字符串的长度。复制时, 包含第index个字符.

#include <stdio.h>

void strmcpy (char *str1, int index, char *str2) {
  
    int j = 0;
    int i = index-1;
    for (; str1[i] != '\0'; i++,j++) {
        str2[j] = str1[i];
    }
}
int main () {
    char str1[100] = "I am a student. I like programming.", str2[100];
    int index = 10;
    strmcpy(str1, index, str2);
    printf("%s", str2);
    return 0;
}


运行结果会多出字符

  • 写回答

2条回答 默认 最新

  • hello_world&& 2022-11-25 10:26
    关注

    需要在字符串最后加上'\0'

    #include<stdio.h>
    void strmcpy (char *str1, int index, char *str2) {
        int j = 0;
        int i = index-1;
        for (; str1[i] != '\0'; i++,j++) {
            str2[j] = str1[i];
        }
        str2[j]='\0';
    }
    int main () {
        char str1[100] = "I am a student. I like programming.", str2[100];
        int index = 10;
        strmcpy(str1, index, str2);
        printf("%s", str2);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月25日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来