2017-10-05 15 views
1

Voici mon code: J'ai des problèmes avec l'utilisation de pointeurs. So * string affiche le caractère à l'adresse actuelle, et string + = 1 augmente l'adresse du pointeur pour pointer sur le caractère suivant, mais le programme renvoie un zéro à chaque exécution. À l'origine, j'ai ajouté une autre variable entière à l'adresse, index int, mais quelqu'un m'a dit de la supprimer.En C, comment utiliser les pointeurs pour compter combien de voyelles dans une chaîne?

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; 
} 

Répondre

0

Dans votre programme, par erreur vous avez mis un ";" après en boucle qui est en fait comme en boucle avec le corps vide -

while ((c = getchar()) != '\n'); 

En raison de cette « ; » le code dans le corps de la boucle while ne s'exécutera pas comme prévu. Supprimez le ";" et votre programme fonctionnera.

+0

Cela a corrigé le programme. Je vous remercie! – Gideon

+0

@Gideon Bienvenue à SO, si cette réponse vous a fourni une solution à votre question, veuillez l'accepter, cela vous donnera une certaine réputation, et dites aux futurs lecteurs que cette solution a fonctionné pour vous. –