本文介绍了如何将字符串中的元音更改为符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C将字符串中的元音更改为$.我知道我需要使用for循环,并且我敢肯定我在正确的轨道上,但是我无法使其正常工作

I need to change the vowels in a string to a $ using C. I know that I need to use a for loop and I'm pretty sure I'm on the right track but I can't get it to work.

这是我的代码:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char input[50];
    char i;
    int j = 0;

    printf("Please enter a sentence: ");
    fgets(input, 50 , stdin);

    for (j = 0; input[i] != '\0'; j++)
        if (input[i]=='a'||input[i]=='e'||input[i]=='i'||input[i]=='o'||input[i]=='u')
        {
            input[i]= '$';
            printf("Your new sentence is: %s", input);
        }

    return 0;
}

我知道我的错误不是很大,但是我看不到它.这是家庭作业,所以我不希望有这样的建议作为解决方案,这样我就可以从中学到东西.

I know my error is not a big one but I just can't see it. This is homework so I don't want a solution as such just some advice so that I can actually learn from this.


谢谢那些家伙,我摆脱了"j",它现在可以工作,但是当我运行程序时,它为每一个改变的元音输出一个新行.如何编码,使其仅输出最后一行,即所有元音都已更改?


Thanks for that guys I got rid of 'j' and it now works however when I run the program it outputs a new line for every vowel it changes. How do I code it so that it only outputs the final line i.e. with all of the vowels changed?

推荐答案

您在索引中犯了一个小错误:

You made a small mistake with the index:

for (j = 0; input[i] != '\0'; j++)
     ^                        ^

应该是

for (i = 0; input[i] != '\0'; i++)

实际上,您可以省略j:

int main(void)
{
    char input[50];
    int i;

    printf("Please enter a sentence: ");
    fgets(input, 50 , stdin);

    for (i = 0; input[i] != '\0'; i++)
    {
        if (input[i]=='a'||input[i]=='e'||input[i]=='i'||input[i]=='o'||input[i]=='u')
        {
            input[i]= '$';
        }
    }
    printf("Your new sentence is: %s", input);
    return 0;
}

这篇关于如何将字符串中的元音更改为符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:55