C 行或文本(字符串)中任意两个特定字母出现的次数

C 行或文本(字符串)中任意两个特定字母出现的次数,c,C,问:编写一个程序来计算一行文本中连续出现的任意两个元音的数量。例如下面的句子: "Please read the application and give me gratuity" 这句话中出现的情况是:-“ea”,“ea”,“io”,“ui”。最终的问题是计算用户输入字符串行中此类事件的数量 问题:我的程序刚刚收到一行,但没有给出任何输出 这是我在stackoverflow中的第一个问题。我是编程的初学者 我的代码: # include <stdio.h>

问:编写一个程序来计算一行文本中连续出现的任意两个元音的数量。例如下面的句子:

"Please read the application and give me  gratuity"
这句话中出现的情况是:-“ea”,“ea”,“io”,“ui”。最终的问题是计算用户输入字符串行中此类事件的数量

问题:我的程序刚刚收到一行,但没有给出任何输出

这是我在stackoverflow中的第一个问题。我是编程的初学者

我的代码:

# include <stdio.h>

int main () {
    char line[100];
    printf("\nEnter a line\n");
    gets(line);
    //printf("You entered : %s\n", line);
    char A,E,I,O,U,a,e,J,o,u;
    A = 'A';
    E = 'E';
    I = 'I';
    O = 'O';
    U = 'U';
    a = 'a';
    e = 'e';
    J = 'i';
    o = 'o';
    u = 'u';
    int occurence =0,i =0 ;
    while (line[i] =! '\0'){
        if((line[i] == A || E || I || O || U || a || e || J || o || u) && (line[i+1] == a || e || J || o || u)){
                    occurence++;
        }
        i++;
    }
    printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurence);
    return 0;
}
#包括
int main(){
字符行[100];
printf(“\n输入一行\n”);
获取(行);
//printf(“您输入了:%s\n”,第行);
字符A,E,I,O,U,A,E,J,O,U;
A=‘A’;
E='E';
I=‘I’;
O='O';
U='U';
a=‘a’;
e='e';
J=‘i’;
o='o';
u='u';
int发生率=0,i=0;
而(第[i]行=!'\0'){
如果((行[i]==A | E | i | O | U | A | E | J | O | U)和&(行[i+1]==A | O | E | J | O | U)){
发生++;
}
i++;
}
printf(“行中连续出现的任何两个元音的数量为:%d\n”,出现次数);
返回0;
}

您的代码有多个问题,其中一些是:

  • 使用
    get
    读取字符串。这个函数现在至少被弃用了(我认为它甚至从标准库的最新版本中被删除了),因为它是不安全的。改用
    fgets
  • 您使用的
    |
    运算符错误-这已在MediocrEventTable1的注释中指出
您可以使用类似于此的代码来解决您的问题。代码包含注释,因此应该易于理解。然而,如果这是学校的作业或项目,不要使用这个确切的代码,因为这很可能被认为是剽窃

#include <stdio.h>
#include <ctype.h>

#define STRLEN 100

int main () {
    char line[STRLEN];
    char* ch;
    char incrementIfVowel;
    int occurences;

    /* Read line */
    printf("\nEnter a line\n");
    fgets(line, STRLEN, stdin);

    /* Init variables */
    incrementIfVowel = 0;
    occurences = 0;

    /* Iterate through characters */
    for (ch = line; *ch != '\0'; ch++) {
        /* Test if the current character is a vowel. Three cases can occur: */
        if (toupper(*ch) == 'A' || toupper(*ch) == 'E' || toupper(*ch) == 'I' || toupper(*ch) == 'O' || toupper(*ch) == 'U') {
            if (incrementIfVowel == 1) {
                /* Case 1: The current character is a vowel, and its predecessor was also a vowel */
                incrementIfVowel = 0;
                occurences++;
            }
            else {
                /* Case 2: The current character is a vowel, but its predecessor was not a vowel or it was the second vowel in a row */
                incrementIfVowel = 1;
            }
        }
        else {
            /* Case 3: The current character is not a vowel */
            incrementIfVowel = 0;
        }
    }

    /* Print result */
    printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurences);

    return 0;
}
#包括
#包括
#定义STRLEN 100
int main(){
字符行[STRLEN];
char*ch;
字符递增元音;
事件;
/*读线*/
printf(“\n输入一行\n”);
fgets(线路、STRLEN、stdin);
/*初始变量*/
增量If元音=0;
发生率=0;
/*遍历字符*/
对于(ch=line;*ch!='\0';ch++){
/*测试当前字符是否为元音。可能出现三种情况:*/
如果(toupper(*ch)='A'| | toupper(*ch)='E'| | toupper(*ch)='I'| | toupper(*ch)='O'| | toupper(*ch)='U'){
如果(递增的if元音==1){
/*案例1:当前字符是元音,其前身也是元音*/
增量If元音=0;
事件++;
}
否则{
/*案例2:当前字符是元音,但其前一个字符不是元音或是一行中的第二个元音*/
递增的If元音=1;
}
}
否则{
/*案例3:当前字符不是元音*/
增量If元音=0;
}
}
/*打印结果*/
printf(“行中连续出现的任何两个元音的数量为:%d\n”,出现次数);
返回0;
}

您的代码有三个主要问题:

get(行);
gets
自C99以来已被弃用,并在C11中被删除。您可能正在使用旧版本的标准进行编译;我建议你换成新版本。有关替代方案,请参见

while(第[i]=!'\0'行){
=!
是打字错误。请将其替换为
!=

if((行[i]==A | E | i | O | U | A | E | J | O | U)和&(行[i+1]==A | E | J | O |U))
这将始终计算为true,因为
|
不会像那样连锁。理想情况下,您应该将其放入函数中:

\u Bool是\u元音(char-ch)
{
返回toupper(ch)='A'| | toupper(ch)='E'| | toupper(ch)='I'| | toupper(ch)='O'| | toupper(ch)='U';
}
toupper
是在
中定义的,所以一定要包括它。你也可以用
返回strchr(“AEIOUaeiou”,ch)
来缩短这个庞大的行,但是如果你还没有使用
strchr
,并且还不习惯使用它,那没关系

仅修改不正确的部分,最终代码可能如下所示:

#包括
#包括
_布尔是_元音(char-ch)
{
返回toupper(ch)='A'| | toupper(ch)='E'| | toupper(ch)='I'| | toupper(ch)='O'| | toupper(ch)='U';
}
int main(){
字符行[100];
printf(“\n输入一行\n”);
fgets(生产线、生产线尺寸、标准尺寸);
int发生率=0,i=0;
而(行[i]!='\0'){
if(is_元音(行[i])&&is_元音(行[i+1]))
发生++;
i++;
}
printf(“行中连续出现的任何两个元音的数量为:%d\n”,出现次数);
返回0;
}
运行此命令的示例如下:

输入一行
请对蜂巢做点什么,然后吃些肉
行中连续出现任意两个元音的次数为:5

(5因为
请先做一些关于蜂巢的事,然后做一些mt

line[i]==A | E | i | O | U | A | E | J | O | U
不会做你认为会做的事。你需要做
line[i]=A |=A | E | J |U
。(还有,为什么选择将每个不同大小写的不同元音存储在自己的变量中?为什么不使用普通文字?)。您还可以通过执行
toupper(第[i]行]==A | | toupper(第[i]行])来减少大小写==E…
进行不区分大小写的检查。@mediocrevegetable1但这也不起作用。
行[i]=!'\0'
也不做您认为它能做的事。正确的运算符是
!=