Thread: HELP! (strings)

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    Question HELP! (strings)

    i been asked to write a program that accepts a word into a string variable, and it has to display the number of the vowels and the number of consonants of the word.
    would you help me solve this?
    i don't know how to get the words separated (vowels from consontats)

    #include <iostream.h>

    int main ()
    {
    char word [50];
    char vowel, consonat;

    cout<<"Please enter a word"<<endl;
    cin>>word;

    ???????
    Last edited by mana; 05-08-2002 at 10:05 AM.

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Re: HELP! (strings)

    Originally posted by mana
    i been asked to write a program that accepts a word into a string variable, and it has to display the number of the vowels and the number of consonants of the word.
    would you help me solve this?
    i don't know how to get the words separated (vowels from consontats)

    #include <iostream.h>

    int main ()
    {
    char word, vowel, consonat;

    cout<<"Please enter a word"<<endl;
    cin>>word
    First and foremost, you can't use a character to hold a word. You need to have an array of characters (or a character pointer to an allocated amount of memory). You seem pretty new so I'm just going to suggest that you look up character pointers (char *) and then look at the string object.

    Good luck.

  3. #3
    Unregistered
    Guest
    // This code runs perfect
    // HAVE FUN!! [email protected]

    #include <iostream.h>

    int main ()
    {
    char word [50];
    int vowel=0, consonat=0;
    int count = 0;
    cout<<"Please enter a word: ";
    cin>>word;

    while(word[count] != '\0')
    {
    if((word[count] == 'a')||(word[count] == 'e')||
    (word[count] == 'i')||(word[count] == 'o')||
    (word[count] == 'u'))
    {
    vowel++;
    }
    else
    {
    consonat++;
    }
    count++;
    }
    cout<<"Number of the vowels "<<vowel<<endl;
    cout<<"Number of the consonants "<<consonat<<endl;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >// This code runs perfect

    Unless the CAPS LOCK is on.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here's a simple program to do what you ask. Remember this will only take a word not a string, so if you plan on having spaces this will NOT work, but in your original post you said "word".

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <fstream.h>
    
    int main( void )
    {
      ofstream fout("MyProjects\\info.dat");
      char Array[255];
      int nVowels = 0, nCons = 0;
    
      cout << "Enter a word: ";
      cin >> Array;
    
      for( register int i = 0; i < strlen( Array ); i++ )
      {
           // Convert letter to lower-case
           Array[i] = tolower( Array[i] );
    
           // Switch on the character to determine type
           switch( Array[i] )
           {
                   case 'a':
                        nVowels++;
                        break;
                   case 'e':
                        nVowels++;
                        break;
                   case 'i':
                        nVowels++;
                        break;
                   case 'o':
                        nVowels++;
                        break;
                   case 'u':
                        nVowels++;
                        break;
                   default:
                        nCons++;
                        break;
           }
      }
    
      // Output data to a file
      fout << "Word is " << Array << endl;
      fout << "Number of Vowels - " << nVowels << endl;
      fout << "Number of Consonants - " << nCons << endl;
      fout << "Total letters in word - " << (nCons + nVowels) << endl;
    
      // Close the file after writing to it
      fout.close();
    
      return( 0 );
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    But what happens when you enter a word with non-alpha characters in it.... the counts are lying! for example:

    Word is this.is.a.m3ssage
    Number of Vowels - 5
    Number of Consonants - 12
    Total letters in word - 17
    --------------------------------------------------------
    Word is that's
    Number of Vowels - 1
    Number of Consonants - 5
    Total letters in word - 6
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Hammer
    But what happens when you enter a word with non-alpha characters in it.... the counts are lying! for example:



    That's a very good point. I wrote that program in about 3 minutes without thinking much of user-error. Simply add the following code to execute first in the for loop.
    Code:
    if( !isalpha( Array[i] ))
    {
         continue;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM