【问题标题】:Count the number of vowels in a string计算字符串中元音的数量
【发布时间】:2011-09-05 09:44:29
【问题描述】:

我只是想使用递归计算字符串中的元音,但它不起作用。

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

#define SETSIZ 10

#define TRUE 1
#define FALSE 0
int is_empty(const char *set);
int is_element(char vowel, const char *set);
int is_vowel(const char *vowels, const char *set);

int main(void)
{
    int count = 0,i;
    char vowels[11] = {'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', '\0'}, set[SETSIZ] = "mustafa";
    for(i=0;i<strlen(set);i++){
        if(is_vowel(vowels, set))
            count +=  1;
        }
    printf("%s has %d vowels",set, count);
    return(0);
}
int is_empty(const char *set)
{
    return(set[0] == '\0');
}
int is_element(char vowel, const char *set)
{
    int ans;
    if(is_empty(set))
        ans = FALSE;
    else if (vowel == set[0])
        ans = TRUE;
    else
        ans = is_element(vowel, &set[1]);
    return(ans);
}
int is_vowel(const char *vowels, const char *set)
{
    int ans, i = 0;

    if(is_empty(vowels))
        ans = FALSE;
    else if(is_element(vowels[0], set))
    {
        printf("**");
        ans = TRUE;
    }
    else
    {
        printf("--");
        ans = is_vowel(&vowels[1], set);
        }

    return(ans);
}

【问题讨论】:

  • main 中循环遍历set(在该范围内似乎不存在),并始终将相同的内容传递给is_vowel。你永远不会使用循环计数器i
  • set 在滚动区域中定义。我也认为它一开始没有定义。致 OP:@paranoidgnu -- 将行长限制为 80 个字符 :)

标签: c string recursion


【解决方案1】:

你可以在python中使用这段代码来计算元音的个数:

定义元音(s):

if s == '':
    return 0   # no vowels in the empty string
elif s[0] in 'aeiouAEIOU':
    return 1 + vowels( s[1:] )
else:
    return 0 + vowels( s[1:] )

您也可以使用变量,例如 vowel_list='aeiouAEIOU'

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    
    
    
    int vowel(char str[],int k)
    {
    int count = 0;
    while(str[k]!='\0')
    {
        if(str[k] == 'a' || str[k] == 'e' || str[k] == 'i' || str[k] == 'o' || str[k] == 'u')
            return 1 + vowel(str,k+1);
        else
            return 0 +vowel(str,k+1);
    }
    return 0;
    }
    void main()
    {
    char x[50];
    gets(x);
    printf("%d",vowel(x,0));
    }
    

    【讨论】:

      【解决方案3】:
      1. 您不会像您可能想要的那样循环访问set。应该是:

        if(is_vowel(vowels, &set[i]))
        
      2. 你的函数is_element()是绝对错误的,你可以改成:

        int is_element(char vowel, const char *set)
        {
            return (vowel == set[0]);
        }
        

      甚至传递字符而不是指向字符的指针。

      【讨论】:

        【解决方案4】:

        main 中,您的 for 循环使用完全相同的参数多次调用 is_vowel()

        你可能想用更简单的原型重写函数:

        /* int is_vowel(const char *vowels, const char *set); */
        int is_vowel(const char *vowels, int ch);
        

        【讨论】:

          【解决方案5】:

          您的问题有一个更简单的解决方案:

          #define VOWELS "aeiouAEIOU"
          
          size_t vcount(const char *s)
          {
                  size_t i = 0;
          
                  while (s && *s) {
                          if (strchr(VOWELS, *s)) ++i;
                          ++s;
                  }
          
                  return i;
          }
          

          它可以很容易地转换为递归版本。

          【讨论】:

            【解决方案6】:

            你的 is_vowel 代码有问题。

            int is_vowel(const char *vowels, const char *set)
            {
            int ans, i = 0;
            
            if(is_empty(vowels))     //You are passing vowels which would never be empty.
                ans = FALSE;         //Replace it with set character pointer. 
            //Rest of the code
            

            整个概念,应用似乎是错误的哥们。我建议你重写代码。整个代码中有无数错误。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-06-09
              • 2020-02-19
              • 2020-08-07
              • 1970-01-01
              • 1970-01-01
              • 2020-12-12
              • 2013-11-26
              相关资源
              最近更新 更多