Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Searching for vowels in a Character Array * Stumped *

648 views
Skip to first unread message

Jay Callaghan

unread,
Oct 11, 2000, 3:00:00 AM10/11/00
to
Ok, I am kinda new to this stuff. I got the string converted to a char
array and have a char array defined with the vowels from the alphabet. I am
using the for loop and am stuck on the coding to search the char array for a
vowel. Here is what I have, but it isn't working. Can anyone help a
newbie?

(s1 is the user string (text they entered) defined earlier in the
StringTokenizer section)

char VowelArray[] = {'A', 'E', 'I', 'O', 'U'}; //create character array
for the vowels
char VArray []= s1.toCharArray(); //convert s1 Strin into
VArray[]
int count = 0; //Set
counter of vowels found to 0

for(int i=0; i < VArray.length; i++) //for loop
{
if(VowelArray == VArray[i])
{
count++;
}

}
Output.append("\n" + count + " test"); //print results to Output
TextArea


Now I know in C++ is it something like:

char text[] = //string
char vstr[] = //the vowels

for(i=0; i < strlen(text[j]); j ++)
{
if isalpha(text[j]) != 0)
text[j] = test[j] & 0xdf;
if (strchr(vstr, text[j]) != '\0')
vowels++;
}

Any help would be appreciated. I am just stumped on this. Wondering if I
need to turn a char into an int or the string into an int to compare it,
then back to a string afterwards. Thanx in advance.

Jay Callaghan

unread,
Oct 11, 2000, 3:00:00 AM10/11/00
to
Should also add it is a Java Applet, not application.


Peter van der Linden

unread,
Oct 11, 2000, 3:00:00 AM10/11/00
to
In article <Qp6F5.127788$i5.53...@news1.rdc2.on.home.com>,

Jay Callaghan <jcal...@home.com> wrote:
>Ok, I am kinda new to this stuff. I got the string converted to a char
>array and have a char array defined with the vowels from the alphabet. I am
>using the for loop and am stuck on the coding to search the char array for a
>vowel. Here is what I have, but it isn't working. Can anyone help a
>newbie?

I find it helps to write down in english pseudo-code what I am
trying to do, and then go from that to the code. That separates
issues of WHAT should happen from HOW to express it in a way the
compiler likes.

In this case, I guess the operations would be some thing like:

1. For each char in the vowel array:
2. for each char in the String
3. compare it for match. If it matches
increment the count (Hmmm, need to set that
count to zero somewhere... Before using a new vowel?)

4. Output the count

--
The Java FAQ is at
http://www.afu.com

Might be worth bookmarking it, or looking thru it for other info.

Margalit Gur-Arie_elidan1

unread,
Oct 11, 2000, 3:00:00 AM10/11/00
to
Jay Callaghan wrote:
>
> Should also add it is a Java Applet, not application.

I don't think it's very efficient to translate from one language to
another. Any way there's one quick way you may want to try (there are
others, probably even better)

public class Search{
public static void main(String[] args){
String str="abcdefg";
int count=0;
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'
||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){
count=count+1;
}
}
System.out.println("count "+count);
}
}

Chris Smith

unread,
Oct 11, 2000, 3:00:00 AM10/11/00
to
Jay Callaghan <jcal...@home.com> wrote ...

> Ok, I am kinda new to this stuff. I got the string converted to a char
> array and have a char array defined with the vowels from the alphabet. I
am
> using the for loop and am stuck on the coding to search the char array for
a
> vowel. Here is what I have, but it isn't working. Can anyone help a
> newbie?

The clearest way to do it is to nest another loop that walks your vowel
array and compares to each member. If you want something roughly equivalent
to the C code you provided, you could write:

String s1 = "This is a test string with some UPPERCASE vowels.";
String vowels = "aeiou";
int count = 0;

for (int i = 0; i < s1.length(); i++)
{
char c = Character.toLowerCase(s1.charAt(i));
if (vowels.indexOf(c)) count++;
}

Perhaps this is what you were looking for?

Chris Smith


Mark Charsley

unread,
Oct 13, 2000, 3:00:00 AM10/13/00
to
In article <Qp6F5.127788$i5.53...@news1.rdc2.on.home.com>,
jcal...@home.com (Jay Callaghan) wrote:

> Ok, I am kinda new to this stuff. I got the string converted to a char
> array and have a char array defined with the vowels from the alphabet.
> I am
> using the for loop and am stuck on the coding to search the char array
> for a
> vowel. Here is what I have, but it isn't working. Can anyone help a
> newbie?
>

> (s1 is the user string (text they entered) defined earlier in the
> StringTokenizer section)
>
> char VowelArray[] = {'A', 'E', 'I', 'O', 'U'}; //create character

Even in American/English that's only going to pick up upper case vowels
(easily fixed though). European languages are going to have a load of
accented vowels that you'll miss (substantially less easy to fix, esp
considering that java uses unicode). And as for Aisian languages...

--
Mark (in a personal capacity)

Dirk Bosmans

unread,
Oct 14, 2000, 3:00:00 AM10/14/00
to
I'm reacting to following parts of "Jay Callaghan" <jcal...@home.com>'s
article in comp.lang.java.programmer on Wed, 11 Oct 2000 23:08:32 GMT6

> Ok, I am kinda new to this stuff. I got the string converted to a char
> array and have a char array defined with the vowels from the alphabet. I am
> using the for loop and am stuck on the coding to search the char array for a
> vowel. Here is what I have, but it isn't working. Can anyone help a
> newbie?
>
> (s1 is the user string (text they entered) defined earlier in the
> StringTokenizer section)
>

> char VowelArray[] = {'A', 'E', 'I', 'O', 'U'}; //create character array
> for the vowels
> char VArray []= s1.toCharArray(); //convert s1 Strin into
> VArray[]
> int count = 0; //Set
> counter of vowels found to 0
>
> for(int i=0; i < VArray.length; i++) //for loop
> {
> if(VowelArray == VArray[i])
> {
> count++;
> }
>
> }
> Output.append("\n" + count + " test"); //print results to Output
> TextArea
>
>
> Now I know in C++ is it something like:
>
> char text[] = //string
> char vstr[] = //the vowels
>
> for(i=0; i < strlen(text[j]); j ++)
> {
> if isalpha(text[j]) != 0)
> text[j] = test[j] & 0xdf;
> if (strchr(vstr, text[j]) != '\0')
> vowels++;
> }
>
>
>
> Any help would be appreciated. I am just stumped on this. Wondering if I
> need to turn a char into an int or the string into an int to compare it,
> then back to a string afterwards. Thanx in advance.
>
>
>

Apart from the solution you try to implement, the implementation is not correct.
You compare an array (VowerArray) to a char (VArray[i]). Try to compare char to
char and you'll get a bit further.


Greetings,
Dirk Bosmans


http://users.belgacombusiness.net/arci/
- Applicet Framework: turns Applets into Applications
- ArciMath BigDecimal: now with BigDecimalFormat

Dale King

unread,
Oct 16, 2000, 3:00:00 AM10/16/00
to
Chris Smith wrote:
>
> The clearest way to do it is to nest another loop that walks your vowel
> array and compares to each member. If you want something roughly equivalent
> to the C code you provided, you could write:
>
> String s1 = "This is a test string with some UPPERCASE vowels.";
> String vowels = "aeiou";
> int count = 0;
>
> for (int i = 0; i < s1.length(); i++)
> {
> char c = Character.toLowerCase(s1.charAt(i));
> if (vowels.indexOf(c)) count++;
> }

That should be:

if( vowels.indexOf( c ) != -1 )
{
count++;
}

--
--- Dale King

James Kanze

unread,
Oct 17, 2000, 3:00:00 AM10/17/00
to
Jay Callaghan wrote:

> Now I know in C++ is it something like:

> char text[] = //string
> char vstr[] = //the vowels

> for(i=0; i < strlen(text[j]); j ++)
> {
> if isalpha(text[j]) != 0)
> text[j] = test[j] & 0xdf;
> if (strchr(vstr, text[j]) != '\0')
> vowels++;
> }

This won't always work in C++, either, since C++ doesn't specify the
character code used. Not to mention that it modifies the text string
it is given (rather unexpected if the goal is to count vowels). And
if text is of any length at all, calling strlen each time through is a
no-no.

Basically, I'd handle this the same way in both languages, at least as
a first draft:

boolean
isVowel( char ch )
{
// For 8 bit C++ char's, I'd probably define a bit array, or
// maybe even a bool array. For 16 bit Java characters, or
// wchar_t in C++, however...
return "AEIOUYaeiouy\u00C0\u00C1\u00C2...".indexOf( ch ) != -1 ;
}

int
vowelCount( String text )
{
int result = 0 ;
for ( int i = 0 ; i < text.length() ; ++ i ) {
if ( isVowel( text.charAt( i ) ) ) {
++ result ;
}
}
return result ;
}

(Or perhaps in C++, I'd try and use std::for_each. Not a job for
beginners, however.)

More generally:

- The problem is under-specified. Is 'r' a vowel? Not in English,
perhaps, but it certainly is in Croatish. So isVowel has to be
locale dependant. Or worse, is Y a vowel? It is in "my"; it
isn't in "yes". Handling that correctly will require some pretty
fancy logic. (First guess: if y precedes a vowel, it is a
consonant, otherwise it is a vowel. Of course, at best, this
works only for English. In most European languages, it is always
a vowel, but in Hungarian, it always a consontant, Except in
proper names.

- Obviously, you're going to have problems if you encounter any
idiograms (CJK characters), or even sylabic scripts. The number
of vowels in the "character" may be more than one, and may depend
on which language it is spoken. Arabic poses a different problem
-- in classical Arabic, the vowels aren't written.

- The length of the string being searched in isVowel may be quite
long, and indexOf does a linear search. You might want to use
binary search, or simply break off the search as soon as you see
an entry whose numeric value is greater than that of the character
being tested. (In this special case, I suspect that this second
solution is typically faster than a binary search.)

--
James Kanze mailto:ka...@gabi-soft.de
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelhüttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

0 new messages