loops (words with vowels...)

Finding the number of words in a sentence starting with vowels and con

Page 1 of 1

5 Replies - 3236 Views - Last Post: 12 October 2009 - 02:06 PM Rate Topic: -----

#1 conditionzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 09

loops (words with vowels...)

Post icon  Posted 08 October 2009 - 11:00 PM

So i have a homework problem and i am not sure how to start it.

Write a program that reads input a word at a time until a lone q is entered. The program
should then report the number of words that began with vowels, the number that began
with consonants, and the number that fit neither of those categories. One approach is to
use isalpha() to discriminate between words beginning with letters and those that
donít and then use an if or switch statement to further identify those passing the
isalpha() test that begin with vowels. A sample run might look like this:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others

Thanks for the help

Is This A Good Question/Topic? 0
  • +

Replies To: loops (words with vowels...)

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5232
  • View blog
  • Posts: 27,023
  • Joined: 10-May 07

Re: loops (words with vowels...)

Posted 08 October 2009 - 11:06 PM

View Postconditionzero, on 9 Oct, 2009 - 12:00 AM, said:

So i have a homework problem and i am not sure how to start it.

Can you at least show us what you have? Surely you can give an effort 1st.
Was This Post Helpful? 0
  • +
  • -

#3 conditionzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 09

Re: loops (words with vowels...)

Posted 08 October 2009 - 11:25 PM

#include <iostream>
#include <cctype>

using namespace std;


void inspectString();
int vowels,consonants;
const int size = 101;
char userstring[size];

int main()
{
bool ok;

cout << "This program will count the number of consonants and vowels in your string, up to 100 characters. " << endl << endl;


cout << "Enter your string: ";
cin.getline(userstring, size);



char c;

while (ok){


cout << "Would you like to: " << endl;
cout << "A) Count the number of vowels in the string? " << endl;
cout << "B) Count the number of consonants in the string? " << endl;
cout << "C) Count both the vowels and consonants in the string? " << endl;
cout << "D) Enter another string? " << endl;
cout << "E) Exit the program? " << endl << endl;
cout << "Choice : ";
cin.get©;
cin.ignore();

//make any lowercase letters into uppercase
if (c >= 'a' && c <= 'z'){
c -= 'a' - 'A';
}


inspectString();

if (c=='A')
{

cout<<"Number of vowels: "<<vowels<<endl<<endl;

}
else if(c=='B')
{

cout<<"Number of consonants: "<<consonants<<endl;

}
else if(c=='C')
{

cout<<"Number of consonants and vowels: "<<consonants+vowels<<endl<<endl;

}
else if (c=='D')
{


cout << "\n\nEnter your string: ";
cin.getline(userstring, size);


}
else if (c=='E')
{

ok=false;

}
}
}
void inspectString()
{

vowels=0;
consonants=0;


for(int i=0;i<strlen(userstring);i++)
{


//convert all letters to uppercase
if (userstring[i] >= 'a' && userstring[i] <= 'z')
userstring[i] -= 'a' - 'A';


if(userstring[i] >= 'A' && userstring[i] <= 'Z')
{


if(userstring[i] == 'A' || userstring[i] == 'E' || userstring[i] == 'I' || userstring[i] == 'O' || userstring[i] == 'U')
{

vowels++;


}
else
{


consonants++;

}

}

}

}



This one just counts all the vowels / consonants in the whole string, all i want to do is basicaly count the number of words starting with viwels / consonants and neither.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: loops (words with vowels...)

Posted 08 October 2009 - 11:31 PM

So, what's your question?
Was This Post Helpful? 0
  • +
  • -

#5 Zardinus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 14-May 09

Re: loops (words with vowels...)

Posted 09 October 2009 - 04:08 PM

I believe you can define a word as character prefixed with space ..

so .. this may helps you try it out

if((userstring[i-1] == ' ') && (userstring[i] == 'A' || userstring[i] == 'E' || userstring[i] == 'I' || userstring[i] == 'O' || userstring[i] == 'U'))
{

vowels++;


}
else if(userstring[i-1] == ' ')
{

consonants++;

}


This code will counts the words with vowels and non-vowel letters

This post has been edited by Zardinus: 09 October 2009 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 conditionzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 09

Re: loops (words with vowels...)

Posted 12 October 2009 - 02:06 PM

int main()
{
string word;

int vowels = 0;
int consonants = 0;
int neither = 0;

for (int i=0; i<1000; i++)
{
cout << "Enter a word (enter q to quit): " ;
cin >> word;

if (word == "q")
{
break;
}
else
{
if (isalpha(word[i]))
{
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u' ||
word[i] == 'A' || word[i] == 'E' || word[i] == 'I' || word[i] == 'O' || word[i] == 'U')
{
vowels++;
}

else
{
consonants++;
}
}

else
{
neither++;
}

}
}

cout << "Number of Vowels: " << vowels << endl;
cout << "Number of Consonants: " << consonants << endl;
cout << "Number of Others: " << neither << endl;
return 0;
}







Can anybody tell me whats wrong with this code? it doesnt give the right answer all the time.
my teacher changed his office hours and this is due tomorrow midnight.
thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1