C语言输入字符串,删除指定字符

用C语言从键盘输入5个整数,删除所需索引的元素,输入删除后的剩余的数组元素,然后继续删除所需索引的元素,直到所有的元素值为0,程序结束。
例如
      输入:8 4 6 1 2
      输入要删除的索引:3
      8 4 6 2 0
      输入要删除的索引:0
      4 6 2 0 0
      输入要删除的索引:1
      4 2 0 0 0
      输入要删除的索引:0
      2 0 0 0 0
     输入要删除的索引:0
     0 0 0 0 0

#include<stdio.h>
int main() {
    int n;
    int a[5];
    printf("输入:");
    for (int i = 0; i < 5; i++)
    {
        scanf("%d",&n);
        a[i] = n;
    }
    int m = 5;
    while (m>0)
    {
        printf("输入要删除的索引:");
        scanf("%d",&n);
        if (n<0 || n>=m)
        {
            printf("输入的索引错误\n");
            continue;
        }
        for (int i = n; i < m-1; i++)
        {
            a[i] = a[i+1];
        }
        m--;
        a[m] = 0;
        for (int i = 0; i < 5; i++)
        {
            printf("%d ",a[i]);
        }
        printf("\n");
    }
}

 

 

思路

1.定义 int a[5];

2.for循环输入5个元素

3.do...while循环,删除指定索引元素。

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y

C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html