the folllowing code i have made but its not catering for the vowels coming together...what should i do in this code to accurately count the number of vowels and count the remining char left in string without vowels as after tokenization only the first token remains in the char array/string being tokenized...plz helpInline Code Example Here

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    int vowelcounter=0;
    char * tokenPtr;
    char myarray[]="My name is Faraz Saleem Rafai";
    char check[]="'a','e','i','o','u'";
    tokenPtr=strtok(myarray,check);
    while(tokenPtr!=NULL)
    {   
        cout<<tokenPtr<<endl;
        tokenPtr=strtok(NULL,check);
        vowelcounter++;
    }
    cout<<myarray<<endl;
    cout<<vowelcounter<<endl;
    return 0;



}

Why not just keep it simple?

int vowels = 0;
int other = 0;

for (int i = 0; myarray[i] != '\0'; i++) {
    switch (myarray[i]) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        ++vowels;
        break;
    default:
        ++other;
        break;
    }
}

cout << "Vowels: " << vowels << '\n' 
     << "Other: " << other << '\n';
Comments
Kudos

char check[]="'a','e','i','o','u'";

That does not produce a null-terminated string, which strtok() requires. Just do this:
char check[] = "aeiou";

But --- Deceptikon has a better solutiom.

but my task was to do by using strtok() fn....so kindly help me on that....the code u have placed i know abt it but the problem is in making through strtok. and ancient dragon the way ur saying to initialize,i also did that,it does not give the correct count of vowels even then...so help please

but my task was to do by using strtok() fn

I love it when bad teachers ask you to do something with an approach that's woefully unsuited to it. :rolleyes: strtok isn't designed to "find" things, and any solution that attempts to use it that way is stupid.

You encountered one such reason for that. strtok doesn't recognize adjacent delimiters as being unique. There's no way around this without eschewing strtok and using some other ad hoc or non-standard tokenization library.

If I haven't been clear, strtok is the wrong solution. You cannot solve this problem just with strtok, period.

strtok() cannot be used the way you are trying to do it, you can't use it to count the repeating adjacent vowels because of the way strtok() works.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

What you might do is to count all the characters in the strings returned by strtok(), then subtract that from the length of the string. The remainder is the number of vowels in the string. So, instead of counting vowels you need to count non-vowels.

ancient dragon ur right but even by that way it does not gives the accurate count for vowels who repeat consecutively....well bad teacher is one aspect of deceptikon.....please help on it if u can..

Comments
I'm sorry I tried to help you. I won't make that mistake again.

Then you did something wrong. This works perfectly

#include<iostream>
#include<cstring>
using namespace std;
#pragma warning(disable: 4996)

int main()
{
    int vowelcounter = 0;
    char * tokenPtr;
    char myarray [] = "My name is Faraz Saleem Rafai";
    char check [] = "aeiou";
    int length = 0;
    int origlength = strlen(myarray);
    // display all the vowels.  Your program doesn't need this part, 
    // it's here only to verif the results are right or wrong.
    for (int i = 0; myarray[i] != 0; i++)
    {
        myarray[i] = tolower(myarray[i]);
        if (strchr(check, myarray[i]) )
            cout << myarray[i];
    }
    cout << '\n';

    tokenPtr = strtok(myarray, check);
    while (tokenPtr != NULL)
    {
        length += strlen(tokenPtr);
        tokenPtr = strtok(NULL, check);
    }
    cout << "orig length: " << origlength << " numvowels = " << origlength - length << '\n';
    return 0;



}

Edited 2 Years Ago by Ancient Dragon

awesome program ancient dragon u rock.....thanks alot...that was a great help......

This question has already been answered. Start a new discussion instead.