Can you help me please?

Joined
Feb 2, 2023
Messages
3
Reaction score
0
Enter text. Determine the percentage of use of each of the vowel letters in it (take into account lower and upper case letters). Record the result in descending order.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
Enter text. Determine the percentage of use of each of the vowel letters in it (take into account lower and upper case letters). Record the result in descending order.
Here's a code snippet in C that takes a string as input and outputs the percentage of use of each vowel letter in the string:

C:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_LENGTH 1000

int count_vowels(const char *str) {
  int count = 0;
  for (int i = 0; i < strlen(str); i++) {
    char c = tolower(str[i]);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
      count++;
    }
  }
  return count;
}

int main() {
  char str[MAX_LENGTH];
  int vowels[5] = {0};
  char vowels_order[5] = {'a', 'e', 'i', 'o', 'u'};
  printf("Enter a string: ");
  fgets(str, MAX_LENGTH, stdin);

  int total_vowels = count_vowels(str);
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < strlen(str); j++) {
      char c = tolower(str[j]);
      if (c == vowels_order[i]) {
        vowels[i]++;
      }
    }
  }

  printf("Vowel usage in descending order: \n");
  for (int i = 4; i >= 0; i--) {
    float percentage = (float)vowels[i] / total_vowels * 100;
    printf("%c: %.2f%%\n", vowels_order[i], percentage);
  }
  return 0;
}

This program uses the tolower function from the ctype.h library to convert the input string and the vowels to lower case letters, so that both upper and lower case vowels can be counted. Is this what you looking for ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top