Answer the question
In order to leave comments, you need to log in
How to display the percentage of vowels in a word?
In the task, you need the program to select the word with the most vowels, and then display their percentage in the selected word. The problem is with percentages. I wanted to first count the number of vowels themselves, but here the same number always comes out, and several times. I feel I stumbled on level ground. Maybe there is an easier way?
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include<cstdlib>
#include <iostream>
#include <conio.h>
#define max 150
using namespace std;
int main() {
setlocale (LC_ALL, "RUS");
cout<<"Введите слова: "<<endl;
char input[256];
const char* s = input;
fgets(input, sizeof input / sizeof(char), stdin);
while (*s && !isalpha(*s))
s++;
const char* result = s;
double resultPart = .0;
while (*s) {
//Читаем слово.
const char* wordStart = s;
int vowelCount = 0, consonantCount = 0;
while (*s && isalpha(*s)) {
if (strchr("aeiouy", *s))
vowelCount++;
else
consonantCount++;
s++;
}
double part = (double)vowelCount / (vowelCount + consonantCount);
if (part > resultPart) {
resultPart = part;
result = wordStart;
}
while (*s && !isalpha(*s))
s++;
int n, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0;
char result[max];
for(n=0;n!=max;n++)
{
if (result[n]=='a')
c1=c1+1;
if (result[n]=='e')
c2=c2+1;
if (result[n]=='y')
c3=c3+1;
if (result[n]=='u')
c4=c4+1;
if (result[n]=='i')
c5=c5+1;
if (result[n]=='o')
c6=c6+1;
};
{
cout<<"Кол-во: "<<endl;
cout<<("%d,%d,%d,%d,%d,%d",c1,c2,c3,c4,c5,c6)<<endl;
}
}
cout<<"Результат: "<<endl;
while (*result && isalpha(*result))
putchar(*result++);
putchar('\n');
}
Answer the question
In order to leave comments, you need to log in
You have two result variables. And where you count the number of letters - the result variable is empty.
Take a debugger and walk through the program - and much will become clear.
an easier way is to use the standard C++ containers and algorithm library. like this:
pastebin.com/Hiy2m5AQ
The algorithm is simple. Two arrays, vowels in one, consonants in the other. We are looking for consonants_count++, or vowels_count++. We count percentages. Everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question