Functions & Array Parameters.

Hello.

I've been stuck on this question for a while... I got this question in my Assignment:

Write a LetterCount function (and the main() function to test it) that takes as input a line of text and then it counts and returns the number of vowels and number of non-vowel letters. The line of text is read in the main() function and passed to the LetterCount function, which returns the count of vowels and non-vowel letters. The main() function reads the line of text and prints the counts. Note that the LetterCount function should exclude the count of symbols and spaces.


So my attempt on this worked for counting Vowels only.. I am not really sure if I need to use void because it asks for 2 returns or if I could do nonVowels and Vowels in 1 'int' Function.
Here's my code so far, it only prints the correct number of Vowels:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<cstring>


using namespace std;

int LetterCount(char line[] );

int main()
{
    char line[200];
    int countVowels ;
    int countNonVowels;

    cout<<"Enter a line of text: ";
    cin.get(line, 200);
    cout<<endl;
    countVowels = LetterCount(line);
    cout<<"The number of vowel letters is: "<<countVowels<<endl;
    cout<<"The number of non-vowel letters is: "<<countNonVowels<<endl;
    return 0;
}
int LetterCount(char line[])
{
    int counter;
    int countVowels = 0;
    int countNonVowels = 0;
    int length;

    length = strlen(line);
    for(counter = 0; counter < length; counter++)
    {
        switch(line[counter])
        {
        case 'a': case 'A': case 'e': case 'E':
        case 'i': case 'I': case 'o': case 'O':
        case 'u': case 'U':
            countVowels++;
        }

        countNonVowels = countVowels - counter;

    }
    return countVowels;
}


Do I need to use reference parameters? If so, could someone explain how?
Last edited on
> Do I need to use reference parameters?

In this case, no.
If the function returns the count of vowels, the count of non-vowels can be computed in main.
(count of non-vowels == count of total number of characters in the line - count of characters that are vowels)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<cstring>

// using namespace std; // best avoided

int VowelCount( const char line[] ); // const: make it const-correct

int main()
{
    const int MAX_CHARS = 200 ; // avoid magic numbers
    char line[MAX_CHARS];

    // postpone the declaration of variables till we have a value to initialise them
    // int countVowels ;
    // int countNonVowels;

    std::cout << "Enter a line of text: ";
    std::cin.get( line, MAX_CHARS );

    // http://en.cppreference.com/w/cpp/io/basic_istream/gcount
    const int nchars = std::cin.gcount() ; // the actual number of characters read into line

    const int countVowels = VowelCount(line);

    // every character other than these countVowels characters is a non-vowel
    const int countNonVowels = nchars - countVowels ;

    std::cout << "    The number of vowel letters is: " << countVowels << '\n'
              << "The number of non-vowel letters is: " << countNonVowels << '\n' ;

    // return 0; // there is an implicit return 0 ; at the end of main
}

int VowelCount( const char line[] )
{
    if( line == nullptr ) return 0 ; // sanity check

    int countVowels = 0;

    const int length = std::strlen(line);
    for( int counter = 0; counter < length; ++counter )
    {
        switch( line[counter] )
        {
            case 'a': case 'A': case 'e': case 'E':
            case 'i': case 'I': case 'o': case 'O':
            case 'u': case 'U':
                ++countVowels ;
        }
    }

    return countVowels;
}
(count of non-vowels == count of total number of characters in the line - count of characters that are vowels)

What about non-letters? The OP stated:
"Note that the LetterCount function should exclude the count of symbols and spaces."
> What about non-letters? The OP stated:
> "Note that the LetterCount function should exclude the count of symbols and spaces."

Mea culpa; didn't read the post with care. Thanks!

Updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstring>
#include <cctype>

// count of non-vowel alphanumeric characters passed by reference
int VowelCount( const char line[], int& cnt_alnum_non_wowels ); // const: make it const-correct

int main()
{
    const int MAX_CHARS = 200 ; // avoid magic numbers
    char line[MAX_CHARS];

    std::cout << "Enter a line of text: ";
    std::cin.get( line, MAX_CHARS );

    int countNonVowels ;
    const int countVowels = VowelCount( line, countNonVowels );

    std::cout << "    The number of vowel letters is: " << countVowels << '\n'
              << "The number of non-vowel letters is: " << countNonVowels << '\n' ;
}

int VowelCount( const char line[], int& cnt_alnum_non_wowels )
{
    if( line == nullptr ) { cnt_alnum_non_wowels = 0 ; return 0 ; } // sanity check

    int countTotalAlNum = 0 ;
    int countVowels = 0;

    const int length = std::strlen(line);

    for( int counter = 0; counter < length; ++counter )
    {
        if( std::isalnum( line[counter] ) ) ++countTotalAlNum ;

        switch( line[counter] )
        {
            case 'a': case 'A': case 'e': case 'E':
            case 'i': case 'I': case 'o': case 'O':
            case 'u': case 'U':
                ++countVowels ;
        }
    }

    cnt_alnum_non_wowels = countTotalAlNum - countVowels ;
    return countVowels;
}
This is how I did it, using your code, but rephrasing it and adding a bit to it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<cstring>


using namespace std;

int LetterCount(char line[], int &countNonVowels );

int main()
{
    char line[200];
    int countVowels ;
    int countNonVowels;

    cout<<"Enter a line of text: ";
    cin.get(line, 200);
    cout<<endl;
    countVowels = LetterCount(line, countNonVowels);
    cout<<"The number of vowel letters is: "<<countVowels<<endl;
    cout<<"The number of non-vowel letters is: "<<countNonVowels<<endl;
    return 0;
}
int LetterCount(char line[], int &countNonVowels)
{
    int counter;
    int countVowels = 0;
    int length;
    int count = 0;

    length = strlen(line);

    for(counter = 0; counter < length; counter++)
    {
        switch(line[counter])
        {
        case 'a': case 'A': case 'e': case 'E':
        case 'i': case 'I': case 'o': case 'O':
        case 'u': case 'U':
            countVowels++;
        }
        if (line[counter] >= 'A' && line[counter] <= 'z')
        {
             count++;
        }

    }
    countNonVowels = count - countVowels;

    return countVowels;
}


Thank you guys for helping.
Last edited on
> if (line[counter] >= 'A' && line[counter] <= 'z')

This is not a great idea, favour if( std::isalpha( line[counter] ) )
See: http://en.cppreference.com/w/cpp/string/byte/isalpha
Topic archived. No new replies allowed.