如何替换特定的字符与其他字符的字符串 [英] How to replace specific characters in a string with other characters

查看:82
本文介绍了如何替换特定的字符与其他字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个程序,将作为输入字符串并用*号代替所有的元音。因此,对于世界你好,star_vowels应返回H * LL * W * RLD。

我要什么code到目前为止是:

  INT star_vowels(char中[]){    诠释J;    J = 0;
    而(S [J]!='0'){
        J ++;
        如果(S [J] ='A'|| S [J] =='E'|| S [J] =='我'|| S [J] =='O'|| S [J] = ='U'){
            的putchar('*');
        }其他{
            的putchar(J);
        }
        返回0;
    }
}


解决方案

这code有不对的地方了一些事情。

1)而(S [J]!='0')
我相当肯定要被检查NUL字符,而不是字符常量为零。更改 0 '\\ 0'

2) J ++
你递增j您连看都不看你的数组的第0指数之前。如果你有在s [0]元音,这将被错过。移动 J ++ 来while循环的最底部,刚刚结束的括号之前。

3) S [J] ='A'
您正在使用赋值运算符 = 这里的时候,你应该使用等于运算符 == 来代替。使用赋值运算符是合法的C code,因而将编译。不幸的是,它将返回true,你最终会用星号代替所有字符

4)的putchar(J);
您正试图输出'J'(你的迭代器),当你真的想被输出S [J]。(你的性格)。

5)返回0
就像在#2,你的回报说法是放错了地方。你有它的while循环中时,它应该是的之外的它。你有它编写的方式,而循环将你的函数之前,只执行第一次迭代超出范围。

  INT star_vowels(char中[]){    INT J = 0;    而(S [J]!='\\ 0'){
        如果(S [J] =='一'|| S [J] =='E'|| S [J] =='我'|| S [J] =='O'|| S [J]。 =='U'){
            的putchar('*');
        }其他{
            的putchar(S [J]);
        }
        J ++;
    }
    返回0;
}

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".

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;
    }
}

解决方案

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'

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.

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

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

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

int star_vowels(char s[]) {

    int j = 0;

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

这篇关于如何替换特定的字符与其他字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆