Need Help on Arrays

  • Thread starter Thread starter Guest
  • Start date Start date
A counter is usally an int that counts something. An array is just a list of integers of a specific size. The following example creates an array of counters and counts the number of each vowel in a string

void count_vowels(char* str)
int counts[5]

counts[0] = 0
counts[1] = 0
counts[2] = 0
counts[3] = 0
counts[4] = 0

for (; str[0] != '\0'; str++)
if (str[0] == 'a')
counts[0]++

if (str[0] == 'e')
counts[1]++

if (str[0] == 'i')
counts[2]++

if (str[0] == 'o')
counts[3]++

if (str[0] == 'u')
counts[4]++



cout<<"Count of 'a' "<<counts[0]<<endl
cout<<"Count of 'e' "<<counts[1]<<endl
cout<<"Count of 'i' "<<counts[2]<<endl
cout<<"Count of 'o' "<<counts[3]<<endl
cout<<"Count of 'u' "<<counts[4]<<endl
}
 
Back
Top