首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2013-09-17 
在字符串中去除指定的字符#include stdio.hvoid squeeze(char s[], int c)/* delete all c from s */in

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


#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的类型会有影响? string
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

#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)

这样是可以的。我是想知道为什么原来那样不可以。^_^



哦哦  不好意思 没看你下面文字说明的  以为你让改错的。
你把c输入那个改成scanf("%d", &c);
然后输入c的时候  按照ascii码对应的输入就好。或者你那自定义函数那边转换一下

热点排行