Reputation: 31
int countConsonant(string str, int consonant)
{
int length = str.length();
consonant = 0;
for (int i = 0; i < length; i++)
{
if(str[i] == 'b'&& str[i] == 'c' && str[i] == 'd'&& str[i] == 'f'
&& str[i] == 'g'&& str[i] == 'h'&& str[i] == 'j' && str[i] == 'k'&& str[i] == 'l'&& str[i] == 'm'
&& str[i] == 'n'&& str[i] == 'p'&& str[i] == 'q'&& str[i] == 'r'&& str[i] == 's'&& str[i] == 't'
&& str[i] == 'v'&& str[i] == 'w'&& str[i] == 'x'&& str[i] == 'y'&& str[i] == 'z')
consonant = consonant + 1;
}
return consonant;
}
Upvotes: 0
Views: 6804
Reputation: 1
You can use the following to count how many consonants are in a string:
int main()
{
char a[20];
int consonants;
gets(a);
cout<<a<<endl;
if(a[i]>=0 && a[i]<=9) // consonants
{
consonants++;
}
cout<<"Total Consonats are: "<<consonants;
}
Upvotes: 0
Reputation: 106196
Way overkill for this homework, but just for reference - an object-oriented approach:
struct Consonants
{
bool x_[256];
Consonants()
{
// constructor sets x_[i] to true or false to indicate
// whether character with ASCII number i is a consonant...
for (int i = 0; i < 256; ++i)
x_ = false;
// note: ASCIIZ string finishes with NUL, so *p becomes false
// and for loop exits
for (const char* p = "bcdfghjklmnpqrstvwxyz"
"BCDFGHJKLMNPQRSTVWXYZ"; *p; ++p)
x_[*p] = true;
};
int in(const std::string& str) const
{
int result = 0;
for (int i = 0; i < str.size(); ++i)
if (x_[(unsigned char)str[i]])
++result;
return result;
}
};
Usage:
Consonants consonants; // create an "utility" object once
int c1 = consonants.in("hello world"); // use as often as desired
int c2 = consonants.in("goodbye cruel world");
Upvotes: 0
Reputation: 106196
You were close, but should check with logical-or ||
, not logical-and &&
(str[i]
simply can't be equal to two different things).
The C++03 Standard allows you to use the keywords and
and or
- and not
, xor
etc - instead of these cryptic (for new programmers) symbols, but this hasn't caught on widely - perhaps because Microsoft's compiler doesn't default to being Standard-compliant in this regard - presumably to avoid breaking existing client code that has variables, functions, types etc. with these names. So, for portability and simplicity, many libraries and textbooks avoid these keywords too.
Another approach that might be a little more concise is to use isalpha()
from <cctype>
then check it's not a vowel. Faster approaches tend to use arrays from character value to bool
, but beware indexing outside the array due to signed character values or >=128 bit non-ASCII values. If there's also uppercase/lowercase to consider - you may want to use tolower()
on your character before testing it (i.e. char c = tolower(str[i])); if (c == '...
).
Other notes: your function should:
std::string
argument by const
reference (i.e. const std::string& str
) to avoid unnecessary and time-consuming copying of the value from the calling context into a separate variable local to this function. The copying doesn't do any real functional harm, but it's unnecessary.consonant
a local variable rather than a function parameter, as any inputted value is immediately clobbered with 0, and the result it returned by the function rather than written into consonant
(which would be impossible as it is passed by value rather than passed by pointer/reference).Upvotes: 8
Reputation: 12170
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
size_t countConsonant(const string& s) {
const string vowels("aeiou");
size_t count = 0;
for (string::const_iterator i = s.begin(); i != s.end(); ++i) {
if (vowels.find(tolower(*i)) == string::npos) {
++count;
}
}
return count;
}
int main() {
cout << countConsonant("abcd") << endl;
}
Or, if you don't want non-alpha stuff to match, you can do it like this:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
size_t countConsonant(const string& s) {
const string cons("bcdfghjklmnpqrstvwxyz");
size_t count = 0;
for (string::const_iterator i = s.begin(); i != s.end(); ++i) {
if (cons.find(tolower(*i)) != string::npos) {
++count;
}
}
return count;
}
int main() {
cout << countConsonant("abcd") << endl;
}
Upvotes: 0
Reputation: 35505
How about this:
#include <algorithm>
#include <iostream>
#include <string>
bool isVowel(char c)
{
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
{
return true;
}
default:
{
return false;
}
}
}
bool isConsonant(char c)
{
return !isVowel(c);
}
int main(int argc, char *argv[])
{
std::string test = "hello";
std::size_t numVowels = std::count_if(test.begin(), test.end(), isConsonant);
std::cout << "Number of vowels is: " << numVowels << std::endl;
return 0;
}
Upvotes: 3
Reputation: 131829
First: &&
will test everything until something returns false
or the end is reached. What you want is ||
which will test until a condition is true
.
Second: What is shorter? Testing for consonants or testing for vowels? Vowels of course. Just put that test in an extra function (note: I assume a valid, alphabetical input string here):
function is_vowel(c) : bool
for each vowel test
if c == that vowel
return true
return false
After that, just replace your big conditional statement with a simple !is_vowel(str[i])
. :) And last but not least, you want to increment your consonant
variable, and there is a special operator for that: the increment operator! (Cool name, huh?)
++consonant; // better than consonant = consonant + 1; but same result
Upvotes: 2
Reputation: 10871
For starters you are using &&
(and) which is a logic error in this case. You want to use ||
(or).
You can also shorten the code a great deal by checking to see if they are NOT vowels.
int countConsonant(string str, int consonant)
{
int length = str.length();
int consonant = 0;
for (int i = 0; i < length; i++)
{
if(!(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u'))
consonant = consonant + 1;
}
return consonant;
}
Someone else already posted an alternate form of this code as a function where you just pass in the variable you want to test.
Upvotes: 0
Reputation: 39882
Look into regular expressions. It allows you to specify a list of characters to be checked for. You would check each character in the string against the list and increment your counter.
http://www.johndcook.com/cpp_regex.html
Upvotes: 0