count vowels from string using isvowel, how do get vowels from the string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie318
    New Member
    • Oct 2011
    • 4

    count vowels from string using isvowel, how do get vowels from the string

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool isVowel(char);
    
    int main ()
    {  
       // Declare variables.
       string word;
       char ch;  
       int numOfVowel = 0;  
       int vowel; 
        
       // Get letters from the user. 
       cout << "Please enter a sequence of letters: " << endl << ">";  
       cin >> word; 
       vowel = isVowel(ch);
        
       // Loop to grab all letters.
       while (ch != '\n')  
        {  
           if (vowel == true) 
           {     
           numOfVowel++;  
           cin.get(ch); 
           }
        }  
       // Output statment to user with number of vowels found.
       cout << "The number of vowels is sequence is: " << numOfVowel << endl;      
       
       system("pause");  
       return 0;  
       
       // Function to find the vowels.
    }
    bool isVowel(char ch)
    {
         if ('A' == ch || 'a' == ch ||'E' == ch || 'e' == ch ||'I' == ch ||
     	    'i' == ch ||'O' == ch || 'o' == ch ||'U' == ch || 'u' == ch) 
         {
         return true;
         }
         else return false;        
    }
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    In C/C++ string array is null terminated, it means the string will be considered end if the default string processing functions found 0(integer zero, not character zero). If it dont find the 0 it will continue its process, and may cause segmentation fault.


    Here is a simple method of how to access all the character individually in a string(char array)


    Code:
    for(i=0;a[i]!=0; i++)
    {
     printf("%c",a[i]);
    }
    Last edited by johny10151981; Oct 20 '11, 07:27 AM. Reason: I didnt notice that you used string not char *

    Comment

    Working...