Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中,如何使用指针计算字符串中的元音数?_C_Pointers_Counting - Fatal编程技术网

在C语言中,如何使用指针计算字符串中的元音数?

在C语言中,如何使用指针计算字符串中的元音数?,c,pointers,counting,C,Pointers,Counting,这是我的密码: 我在使用指针方面有问题。因此,*string显示当前地址处的字符,string+=1增加指针的地址以指向下一个字符,但程序每次执行时都返回零。最初,我在地址中添加了另一个整数变量int index,但有人让我删除它 void menu(); int vowels(char *); int consonants(char *); void CtoUpper(char *); void CtoLower(char *); void displayString(char *); in

这是我的密码: 我在使用指针方面有问题。因此,*string显示当前地址处的字符,string+=1增加指针的地址以指向下一个字符,但程序每次执行时都返回零。最初,我在地址中添加了另一个整数变量int index,但有人让我删除它

void menu();
int vowels(char *);
int consonants(char *);
void CtoUpper(char *);
void CtoLower(char *);
void displayString(char *);

int main()
{
 menu();
}

void menu()
{
 char *string, string_holder[100], choice, input, c = ' ';

 int index = 0;

 printf("Please enter a string:");
 while (( c = getchar() ) != '\n');
 {
      string_holder[index] = c;
      index++;
 }
 string_holder[index] = '\0';
 choice = ' ';
 string = string_holder;

 while (choice != 'X')
 {
      printf("                     Menu\n");
      printf("-------------------------------------------------\n");
      printf("A) Count the number of vowels in the string\n");
      printf("B) Count the number of consonants in the string\n");
      printf("C) Convert the string to uppercase\n");
      printf("D) Convert the string to lowercase\n");
      printf("E) Display the current string\n");
      printf("X) Display the current string\n\n");

      scanf(" %c", &choice);

      if (choice == 'A')
      {
           printf("The number of vowels in the string is ");
           printf("%d\n\n", vowels(string) );
      }
      /*else if (choice == 'B')
      {
           printf("The number of consonants is in the string is ");
           printf("%d\n\n", consonants(string) );
      }
      else if (choice == 'C')
      {
           CtoUpper(string);
           printf("The string has been converted to uppercase\n\n");
      }
      else if (choice == 'D')
      {
           CtoLower(string);
           printf("The string has been converted to lowercase\n\n");
      }
      else if (choice == 'E')
      {
           printf("Here is the string:\n");
           displayString(string);
      }*/
 }
}

int vowels(char *string)
{
 int number_of_vowels = 0;

 while (*string != '\0')
 {
      switch (*string)
      {
           case 'a':
           case 'A':
           case 'e':
           case 'E':
           case 'i':
           case 'I':
           case 'o':
           case 'O':
           case 'u':
           case 'U':
                number_of_vowels += 1;
                break;
      }
      string++;
 }
 return number_of_vowels;
}

在您的程序中,您错误地在while循环之后放置了一个“;”使其成为具有空主体的while循环-

while (( c = getchar() ) != '\n');
因此,while循环体中的“;”代码不会按预期执行。
删除“;”后,您的程序将运行。

在程序中,您错误地在while循环后添加了“;”,使其成为带空主体的while循环-

while (( c = getchar() ) != '\n');
因此,while循环体中的“;”代码不会按预期执行。
删除“;”后,您的程序将运行。

修复了该程序。谢谢大家!@Gideon欢迎这么说,如果这个答案为你的问题提供了一个解决方案,请接受它,这将给你们两个带来一些声誉,并告诉未来的读者这个解决方案对你有效。这修复了程序。谢谢大家!@Gideon欢迎这么说,如果这个答案为你的问题提供了一个解决方案,请接受它,这将给你们两个带来一些声誉,并告诉未来的读者这个解决方案对你有用。