【问题标题】:function not Printing功能不打印
【发布时间】:2022-01-01 12:31:43
【问题描述】:

我需要帮助。 我正在编写一个程序来使用指针和函数计算和打印元音和辅音,我的代码编译成功但不显示结果。

它显示“您的文本计数:0 个元音和 0 个辅音”或“分段错误”

我无法找出问题所在。

 
#include <stdio.h>
#include <string.h>
#define SIZE 1000

void voyelle(char *word)
{
 int i,n,cons=0,vowel=0;
 n=strlen(word);

 while(*word<n)
 {
if((*word>='a'&& *word<='z')||(*word>='A'&& *word<='Z'))
{
  if(*word=='a'|| *word=='A'|| *word=='e'||*word=='E'||*word=='i'||*word=='I'||*word=='o'||*word=='O'||*word=='u'||*word=='U')
{
  vowel++;
}
 else
 cons ++;
}

word++;

 }
 printf(" Your text count:%d vowels and %d consonnants",vowel,cons);
}
int main(void)
{
 char text[SIZE];
 printf("Input your text: ");
 fgets(text,SIZE,stdin);

 voyelle(text);

 return 0;
}
   

【问题讨论】:

    标签: function if-statement pointers while-loop segmentation-fault


    【解决方案1】:

    您的迭代器变量已关闭,而不是执行while(*word &lt; n),而是执行while(i &lt; n) 及以上循环集i = 0 并在循环中执行i++

    【讨论】:

    • 我很感激。按照建议编辑到do-while循环后它只能将整个字符串计为辅音。
    • 请问我的第二个 if 语句有问题吗?如果是,我应该怎么做才能纠正它。
    猜你喜欢
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多