Number of consonants

Help me please! I want to make a program that displays the word with the maximum number of consonants from a sentence.
Can you explain that again, or give an example?
-Read the sentence from somewhere (user input, text file, etc.).
-Split the sentence into words.
-Count consonants for each word and output the one with the most consonants.
For example I type a sentence "I am eating my lunch",as you can see the word "lunch" has the most consonants, so I have to show this word.
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

void maxConsonants(std::istream& stream)
{
	static std::string consonants = "zrtpqsdfghjklmwxcvbn";
	std::string build = "";
	int c = 0;
	int count = 0;

	int max = 0;
	std::string smax = "";

	while ((c = stream.get()) != -1)
	{
		if (c == ' ')
		{
			if (!build.empty() && count > max)
			{
				smax = build;
				build = "";
				max = count;
				count = 0;
			}
			continue;
		}
		if (consonants.find(c) != consonants.npos)	
			++count;
		build += c;
	}
	if (count > max)
		smax = build;
	std::cout << smax;
}


I may forget some consonants
@aquaz: Don't just give out code like that. Too many people will simply take it and leave, learning nothing.
@aquaz: Is this C++? I don't understand this code. Why you didn't use char variables to insert the sentence?
Last edited on
@Answerx: Yup that is C++, I think you are asking for C. You should follow webjose's instructions but here are some more tip for finding the consonants in a given word:-

Use consonant (char array) = ""zrtpqsdfghjklmwxcvbn"
Assuming you have a variable "word" which contain the word which you want to check
Iterate through the word taking one char at a time. Use strstr to check whether it is a consonant or not and record it.
Then return the value from the function. Easy enough!!
Ok I will explain what i am doing:
First, the parameter std::istream& let you perform the function on any stream: a string, a file, stdin... . For example if you want to call the function on a string, you just have to domaxConsonants(std::istringstream("test test2"), on input you domaxConsonants(std::cin)... .
In the while test(l.14), I retrieve the characters from the stream an test if it is the end(-1).
And in the while body, three cases:
1) the char is a space (l.18): I test if the last string builded have the most consonants and if yes if put it as the max string
2)the char is a consonant(l.27): i increment the max count

At the end I output the max string, smax.
Here is a variant made by me, please help me find the mistake.

#include <stdio.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>
#include <string.h>


void main()
{char cons[21]={'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
char s[30],x[30][30],z[30]; int i,j=0,k=0,r,f,n,poz,c,max;
clrscr();
printf("Type sentence:");
gets(s);n=strlen(s);r=0;k=0;

for(i=0;i<n;i++){
if((s[i]!=' ')&&(s[i]!='.')){x[r][k]=s[i];k++;}
if((s[i]==' ')||(s[i]=='.')){x[r][k]='\0';r++;k=0;}}

for(i=0;i<r;i++)
for(j=0;j<strlen(x[i]);j++)
for(k=0;k<21;k++)
if(x[j]==cons[k]) {c++;}
z[i]=c;
max=z[0]; poz=0;
for(i=0;i<r;i++)
if(max<z[i])
{max=z[i];poz=i;}
puts(x[poz]);}
Last edited on
Is there somebody who can help me? Please, I'm desperate.
This sounds like homework, so I'll give you an obfuscated example that your teacher will know you didn't write:

1
2
3
4
5
6
7
int countConsonants(string s) {
    int consonants = 0;
    for(int i = 0;i < s.length();i++) {
        consonants += (s[i] == 'a')?0:((s[i] == 'e')?0:((s[i] == 'i')?0:((s[i] == 'o')?0:((s[i] == 'u')?0:((s[i] == ' ')?0:1)))));
    }
    return consonants;
}


EDIT: Tested now. It works. Updated to not count spaces.
Last edited on
Topic archived. No new replies allowed.