鱼C论坛

 找回密码
 立即注册
查看: 1219|回复: 4

[已解决]删除字符问题

[复制链接]
发表于 2021-10-29 00:14:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
删除字符,若有相同则删除第一个出现字符。

输入
This is a book.
D s
输出
Thi is a book.

大佬们看下为啥删除不了,错在哪了。。。
#include<stdio.h>
#define m 50

char str[m];
int main() {
        int n = 0;
        while (1) {    // 输入字符串
                scanf("%c", &str[n]);
                if (str[n] == '.')
                        break;
                else
                        n++;
        }
        char func, str1, str2;
        scanf("%c%c", &func, &str1);
        if (func == 'D') {     // 删除字符操作
                int num = 0;
                while (str[num] != str1)
                        num++;
                for (num; str[num + 1] != '\0'; num++) {
                        str[num] = str[num + 1];
                }
                str[num] = '\0';
        }
        printf("%s", str);
        return 0;
}
最佳答案
2021-10-29 03:24:24
#include <stdio.h>
#include <string.h>
#define m 50

char str[m];
int main(void)
{
        int n = 0;
        while (1)
        { // 输入字符串
                scanf("%c", &str[n]);
                if (str[n] == '.')
                        break;
                else
                        n++;
        }
        char func, str1, str2;
        //清空输入缓冲区,否则下次输入D s,func得到\n,str1得到D
        while (getchar() != '\n')
                ;
        //两个%c中间加空格,否则输入D空格s,str1得到空格
        scanf("%c %c", &func, &str1);
        if (func == 'D')
        { // 删除字符操作
                int num = 0;
                while (str[num] != str1)
                        num++;
                for (num; str[num + 1] != '\0'; num++)
                {
                        str[num] = str[num + 1];
                }
                str[num] = '\0';
        }
        printf("%s", str);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-29 03:24:24 | 显示全部楼层    本楼为最佳答案   
#include <stdio.h>
#include <string.h>
#define m 50

char str[m];
int main(void)
{
        int n = 0;
        while (1)
        { // 输入字符串
                scanf("%c", &str[n]);
                if (str[n] == '.')
                        break;
                else
                        n++;
        }
        char func, str1, str2;
        //清空输入缓冲区,否则下次输入D s,func得到\n,str1得到D
        while (getchar() != '\n')
                ;
        //两个%c中间加空格,否则输入D空格s,str1得到空格
        scanf("%c %c", &func, &str1);
        if (func == 'D')
        { // 删除字符操作
                int num = 0;
                while (str[num] != str1)
                        num++;
                for (num; str[num + 1] != '\0'; num++)
                {
                        str[num] = str[num + 1];
                }
                str[num] = '\0';
        }
        printf("%s", str);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-29 08:48:24 | 显示全部楼层
#include <stdio.h>

int main()
{
    char str[50];
    for(int i = 0; i < 50; i++){
        scanf("%c", &str[i]);
        if(str[i] == '.') break;
    }
    char a, b;
    getchar(); // 清空缓冲区
    scanf("%c %c", &a, &b); // 这里的 %c 之间必须加空格,因为 %c 不会自动忽略空白键(空白键也是字符,也就是说读取到空白字符 ' ' 了)
    for(int i = 0; str[i]; i++){
        if(str[i] == a) a = '\0';
        else if(str[i] == b) b = '\0';
        else printf("%c", str[i]);
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-29 17:37:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-29 17:37:59 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 19:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表