在字符串中去除指定的字符

BulletInBible 2013-09-16 12:48:01

#include <stdio.h>

void squeeze(char s[], int c);
/* delete all c from s */
int main()
{
char s[50];
int c;

printf("Please enter a string: ");
scanf("%s", s);
getchar();
printf("Enter the c: ");
scanf("%c", &c);
squeeze(s, c);

return 0;
}
void squeeze(char s[], int c)
{
int i, j;

for (i = j = 0; s[i] != '\0'; ++i) {
if (s[i] != c) {
s[j] = s[i];
++j;
}
}
s[j] = '\0';
printf("%s\n", s);
}

在vc++6.0下运行字符串内容不变,把字符c声明和定义中都改成char再跑一遍就可以去除c,求高手解答~我输入的字符都是字母,在squeeze函数中比较时,不都是自动转成ascii码来比较的吗,为什么改变变量c的类型会有影响?
...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BulletInBible 2013-09-16
  • 打赏
  • 举报
回复
引用 4 楼 super_rat 的回复:
[quote=引用 3 楼 u011993989 的回复:] [quote=引用 2 楼 super_rat 的回复:] [quote=引用 1 楼 super_rat 的回复:]
#include <stdio.h>
 
void squeeze(char s[], char c);
/* delete all c from s */
int main()
{
    char s[50];
    char c;
 
    printf("Please enter a string: ");
    scanf("%s", s);
    getchar();
    printf("Enter the c: ");
    scanf("%c", &c);
    squeeze(s, c);
     
    return 0;
}
void squeeze(char s[], int c)
{
    int i, j;
 
    for (i = j = 0; s[i] != '\0'; ++i) {
        if (s[i] != c) {
            s[j] = s[i];
            ++j;
        }
    }
    s[j] = '\0';
    printf("%s\n", s);
}
楼主试试这样呢
把自定义函数体 也改成void squeeze(char s[], char c)[/quote] 这样是可以的。我是想知道为什么原来那样不可以。^_^[/quote] 哦哦 不好意思 没看你下面文字说明的 以为你让改错的。 你把c输入那个改成scanf("%d", &c); 然后输入c的时候 按照ascii码对应的输入就好。或者你那自定义函数那边转换一下[/quote] 明白了!多谢!
max_min_ 2013-09-16
  • 打赏
  • 举报
回复
int c 改成 char c 试试吧!
绝版紫太狼 2013-09-16
  • 打赏
  • 举报
回复
引用 3 楼 u011993989 的回复:
[quote=引用 2 楼 super_rat 的回复:] [quote=引用 1 楼 super_rat 的回复:]
#include <stdio.h>
 
void squeeze(char s[], char c);
/* delete all c from s */
int main()
{
    char s[50];
    char c;
 
    printf("Please enter a string: ");
    scanf("%s", s);
    getchar();
    printf("Enter the c: ");
    scanf("%c", &c);
    squeeze(s, c);
     
    return 0;
}
void squeeze(char s[], int c)
{
    int i, j;
 
    for (i = j = 0; s[i] != '\0'; ++i) {
        if (s[i] != c) {
            s[j] = s[i];
            ++j;
        }
    }
    s[j] = '\0';
    printf("%s\n", s);
}
楼主试试这样呢
把自定义函数体 也改成void squeeze(char s[], char c)[/quote] 这样是可以的。我是想知道为什么原来那样不可以。^_^[/quote] 哦哦 不好意思 没看你下面文字说明的 以为你让改错的。 你把c输入那个改成scanf("%d", &c); 然后输入c的时候 按照ascii码对应的输入就好。或者你那自定义函数那边转换一下
BulletInBible 2013-09-16
  • 打赏
  • 举报
回复
引用 2 楼 super_rat 的回复:
[quote=引用 1 楼 super_rat 的回复:]
#include <stdio.h>
 
void squeeze(char s[], char c);
/* delete all c from s */
int main()
{
    char s[50];
    char c;
 
    printf("Please enter a string: ");
    scanf("%s", s);
    getchar();
    printf("Enter the c: ");
    scanf("%c", &c);
    squeeze(s, c);
     
    return 0;
}
void squeeze(char s[], int c)
{
    int i, j;
 
    for (i = j = 0; s[i] != '\0'; ++i) {
        if (s[i] != c) {
            s[j] = s[i];
            ++j;
        }
    }
    s[j] = '\0';
    printf("%s\n", s);
}
楼主试试这样呢
把自定义函数体 也改成void squeeze(char s[], char c)[/quote] 这样是可以的。我是想知道为什么原来那样不可以。^_^
绝版紫太狼 2013-09-16
  • 打赏
  • 举报
回复
引用 1 楼 super_rat 的回复:
#include <stdio.h>
 
void squeeze(char s[], char c);
/* delete all c from s */
int main()
{
    char s[50];
    char c;
 
    printf("Please enter a string: ");
    scanf("%s", s);
    getchar();
    printf("Enter the c: ");
    scanf("%c", &c);
    squeeze(s, c);
     
    return 0;
}
void squeeze(char s[], int c)
{
    int i, j;
 
    for (i = j = 0; s[i] != '\0'; ++i) {
        if (s[i] != c) {
            s[j] = s[i];
            ++j;
        }
    }
    s[j] = '\0';
    printf("%s\n", s);
}
楼主试试这样呢
把自定义函数体 也改成void squeeze(char s[], char c)
绝版紫太狼 2013-09-16
  • 打赏
  • 举报
回复
#include <stdio.h>
 
void squeeze(char s[], char c);
/* delete all c from s */
int main()
{
    char s[50];
    char c;
 
    printf("Please enter a string: ");
    scanf("%s", s);
    getchar();
    printf("Enter the c: ");
    scanf("%c", &c);
    squeeze(s, c);
     
    return 0;
}
void squeeze(char s[], int c)
{
    int i, j;
 
    for (i = j = 0; s[i] != '\0'; ++i) {
        if (s[i] != c) {
            s[j] = s[i];
            ++j;
        }
    }
    s[j] = '\0';
    printf("%s\n", s);
}
楼主试试这样呢

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧