473,473 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to use string.h to check for vowels and consonants


Hi

How do i use the functions found in string.h to check if a letter is
vowel and a consonant ?
I have this code :

.........
const char vowel[5]="aeiou";
const char consonant[21]="bcdfghjklmnpqrstvwxyz";

while (position<endoffile)
{

read(file,&char_read,1);

if (strpbrk(vowel,char_read)!=NULL)
{
printf("\n vowel ");

}
else if (strpbrk(consonant,char_read)!=NULL)
{
printf("\n consonant ");

}
printf(" %c",char_read);

pozitia_curenta++;
}

}

and somehow the strpbrk does not work .. i also tried with several
functions but i haven't got this right so far

thanks
Vali.

Jan 16 '07 #1
5 5544
"ivalki" <iv****@gmail.comwrote:
How do i use the functions found in string.h to check if a letter is
vowel and a consonant ?
Why are you using those functions? I suggest you use algorithm instead.

#include <algorithm>

bool is_vowel( char c )
{
const char v[11] = "aeiouAEIOU";
return std::find( v, v + 11, c ) != v + 11;
}
I have this code :

........
const char vowel[5]="aeiou";
const char consonant[21]="bcdfghjklmnpqrstvwxyz";
The above two arrays are missing nulls at the end, and they are missing
the upper case versions of the letters.
while (position<endoffile)
{

read(file,&char_read,1);
if (strpbrk(vowel,char_read)!=NULL)
Probably broke because of the missing null.
Jan 16 '07 #2

Daniel T. wrote:
"ivalki" <iv****@gmail.comwrote:
How do i use the functions found in string.h to check if a letter is
vowel and a consonant ?

Why are you using those functions? I suggest you use algorithm instead.

#include <algorithm>

bool is_vowel( char c )
{
const char v[11] = "aeiouAEIOU";
return std::find( v, v + 11, c ) != v + 11;
}
[ snip ]

I would only use an algorithm if I really needed to, and
the use of magic numbers don't really help either :)

bool is_vowel( char c, std::string vowels = "aeiouAEIOU" ) {
return vowels.find_first_of( c ) != std::string::npos;
}

Cheers,
Chris Val

Jan 16 '07 #3

Thanks for helping Chris ( Val ) and Daniel T.

Jan 16 '07 #4
"Chris ( Val )" <ch******@gmail.comwrote:
Daniel T. wrote:
"ivalki" <iv****@gmail.comwrote:
How do i use the functions found in string.h to check if a letter is
vowel and a consonant ?
Why are you using those functions? I suggest you use algorithm instead.

#include <algorithm>

bool is_vowel( char c )
{
const char v[11] = "aeiouAEIOU";
return std::find( v, v + 11, c ) != v + 11;
}

[ snip ]

I would only use an algorithm if I really needed to, and
the use of magic numbers don't really help either :)

bool is_vowel( char c, std::string vowels = "aeiouAEIOU" ) {
return vowels.find_first_of( c ) != std::string::npos;
}
I thought an implementation like that too, but was somewhat concerned
about using a function that creates a string every call in a function
that would likely be used in an inner loop.

Also, your signature allows things like:

if ( is_vowel( c, "!@#$%^&*()" ) )...

Which is, at best, confusing. For your particular implementation, I
suggest a name change to something like "is_one_of" and dump the default
parameter.

Otherwise, I agree with you.

How about a compromise:

bool is_vowel( char c ) {
static const std::string vowels = "aeiouAEIOU";
return vowels.find( c ) != std::string::npos;
}
Jan 16 '07 #5

Daniel T. wrote:
"Chris ( Val )" <ch******@gmail.comwrote:
Daniel T. wrote:
"ivalki" <iv****@gmail.comwrote:
>
How do i use the functions found in string.h to check if a letter is
vowel and a consonant ?
>
Why are you using those functions? I suggest you use algorithm instead.
>
#include <algorithm>
>
bool is_vowel( char c )
{
const char v[11] = "aeiouAEIOU";
return std::find( v, v + 11, c ) != v + 11;
}
[ snip ]

I would only use an algorithm if I really needed to, and
the use of magic numbers don't really help either :)

bool is_vowel( char c, std::string vowels = "aeiouAEIOU" ) {
return vowels.find_first_of( c ) != std::string::npos;
}

I thought an implementation like that too, but was somewhat concerned
about using a function that creates a string every call in a function
that would likely be used in an inner loop.

Also, your signature allows things like:

if ( is_vowel( c, "!@#$%^&*()" ) )...

Which is, at best, confusing. For your particular implementation, I
suggest a name change to something like "is_one_of" and dump the default
parameter.

Otherwise, I agree with you.

How about a compromise:

bool is_vowel( char c ) {
static const std::string vowels = "aeiouAEIOU";
return vowels.find( c ) != std::string::npos;
}
In fact, I was going to offer such an alternative right after
I posted, but was at work and had no time left to post back.

Having said that..., I agree with you're comments in full! :)

Cheers,
Chris Val

Jan 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Prof.Stanley | last post by:
hello , i ma new in this group and writing over the fact that i have a small project at hand,i am writing a program which can permit the input of a text of arbitary lenght,and the number of double...
3
by: Prof.Stanley | last post by:
Hello all, please i am trying to write a program that recieves a string from user and counts double vowels.I have a problem particularly in the way to input the string,below is my code please will...
5
by: Jayson Davis | last post by:
Say I have a string read from a configuration file. searchfor <tab> Needle\n\n Where I want to search for the word "Needle" with two linefeeds. Now when I read it from the file, it hasn't...
3
by: gxs | last post by:
I'm creating a program to convert a .dat file from English to pig Latin under the following rules: 1. If the word begins with a vowel, add "-way" to the end. 2.If the word doesn't begin with a...
12
by: Ron | last post by:
I am getting an error Option strict on disallows implicit conversion from string to long I get it for this code iStartPosition = InStr(iStartPosition + 1, RichTextBox1.Text, "A" Or "a" the...
1
by: dany11gr | last post by:
Hi, I'm actually new to this site. So i don't know if i could ask directly about how to write any code. I'm working on this code to output vowless string from the user- input line of text. For...
14
by: teriwalker | last post by:
import java.util.*; import java.lang.Character.*; public class IsVowel { static char vowels = { 'a', 'e', 'i', 'o', 'u', 'y' }; public static boolean isVowel(char s) {
4
by: Doegon | last post by:
Hi i need help with writing a method that takes in a string of characters and counts the syllables in the string. the method should not count chacter 'e' if its the last character in the string and...
2
by: lpc403 | last post by:
New to Java (and programming in general) and trying to accept a sentence from the command line to count the consonants, vowels, and punctuation. What code do I need to perform the following? 1)...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.