C语言 如何用其他字符替换字符串中的特定字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4243488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:09:32  来源:igfitidea点击:

How to replace specific characters in a string with other characters

c

提问by not_l33t

I am trying to make a program that will take as input a string and replace all the vowels with the * symbol. So, for "hello world", star_vowels should return "h*ll* w*rld".

我正在尝试制作一个程序,该程序将输入一个字符串并用 * 符号替换所有元音。因此,对于“hello world”,star_vowels 应该返回“h*ll* w*rld”。

What I have for code so far is:

到目前为止,我的代码是:

int star_vowels(char s[]){

    int j;

    j = 0;
    while (s[j] != '0'){
        j++;
        if (s[j] = 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u'){
            putchar('*');
        } else {
            putchar(j);
        }
        return 0;
    }
}

回答by SiegeX

This code has a number of things wrong with it.

这段代码有很多问题。

1) while (s[j] != '0')I'm fairly sure you want to be checking for the NUL character, not the character constant zero. Change '0'to '\0'

1)while (s[j] != '0')我很确定您要检查 NUL 字符,而不是字符常量零。更改'0''\0'

2) j++you are incrementing j before you even look at the 0th index of your array. If you had a vowel at s[0], this would be missed. Move j++to the very bottom of the while loop, just before the ending brace.

2)j++在查看数组的第 0 个索引之前,您正在增加 j。如果你在 s[0] 有一个元音,这将被遗漏。移动j++到 while 循环的最底部,就在结束大括号之前。

3) s[j] = 'a'You are using the assignment operator =here when you should be using the equality operator ==instead. Using the assignment operator is legal C code and thus will compile. Unfortunately it will return true and you'll end up replacing all of your characters with asterisks

3)当您应该使用相等运算符时,您在这里s[j] = 'a'使用了赋值运算===。使用赋值运算符是合法的 C 代码,因此可以编译。不幸的是它会返回 true 并且你最终会用星号替换你的所有字符

4) putchar(j);You are attempting to output 'j' (your iterator) when you really want to be outputting s[j] (your character).

4)putchar(j);当您确实想输出 s[j](您的角色)时,您正试图输出 'j'(您的迭代器)。

5) return 0just like in #2, your return statement is in the wrong place. You have it within the while loop when it should be outsideof it. The way you have it written, the while loop will only execute the first iteration before your function goes out of scope.

5)return 0就像在#2 中一样,你的 return 语句在错误的地方。当它应该在它之外时,你在while循环中拥有它。按照您编写的方式,while 循环只会在您的函数超出范围之前执行第一次迭代。

int star_vowels(char s[]) {

    int j = 0;

    while (s[j] != '
char *star_vowels(char s[]){
    // let's allocate memory for the new string.
    // its size should be strlen(s) + 1 (the ending char).
    char *final = malloc(strlen(s) + 1); 

    int j = 0;
    while (s[j] != 0){
        if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u'){
            final[j] = '*';
        } else {
            final[j] = s[j];
        }
        j++;
    }
    final[j] = 0; // end the string
    return final;
}
'){ if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') { putchar('*'); } else { putchar(s[j]); } j++; } return 0; }

回答by wkl

I think your question is going to be 'everything is *' and that's because of this part of your giant if:

我认为您的问题将是“一切都是 *”,这是因为您巨人的这一部分if

if (s[j] = 'a'

if (s[j] = 'a'

That will always be true. You need the ==

这将永远是真的。你需要==

Also you put j++too early - you'd skip characters because you immediately increment upon entering the loop.

j++也太早了 - 你会跳过字符,因为你在进入循环时会立即增加。

回答by Luca Matteis

By incrementing the jat the beginning you will lose the 0index (the first character).

通过j在开头增加 ,您将丢失0索引(第一个字符)。

Since you want to return new data to the outside world (outside the function scope), you either allocate memory for the new data outside the function, and pass in a pointer of that data to this function, or you just allocate dynamic memory inside this function - remember to delete it.

由于您想将新数据返回给外部世界(在函数作用域之外),您要么在函数外部为新数据分配内存,然后将该数据的指针传递给该函数,要么在此内部分配动态内存功能 - 记得删除它。

One implementation would be:

一种实现是:

##代码##

Working example: http://codepad.org/dd2w5cuy

工作示例:http: //codepad.org/dd2w5cuy