How Can I Code a Program Accepts a Pointer to a C-String as its Argument?

I have an assignment that I need help with setting up for an assignment. I need some help finish building the functions. As far as how could I count the number of vowels, consonants, and a combination of vowels and consonants.

And is there anything else that I need to code in that I am missing? And what needs to be coded into the program?


Programming Problem
You will design, code and debug a program that contains functions that accept a pointer to a C-string as its argument. The functions you will code will count the number of vowels, the number of consonants or both in the string depending upon a user’s menu choice (see menu below).

The program flow is:
1. The user is asked to enter a string
2. The program displays the following menu:
a. Count the number of vowels in the string
b. Count the number of consonants in the string
c. Count both the vowels and consonants in the string
d. Enter another string
e. Exit the program

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 <string>

using namespace std;

// function prototypes
countVowels (int);
countConsanants (int);
countVowAndCon (int);
entAntStr (int);

int main()
{
    string countString;
    string anthString;

    cout << "Please type in a sentence about anything: " << endl;
    cin >> countString;

    cout << "Please enter another sentence: " << endl;
    cin >> anthString;

    return 0;
}

// user defined functions
int countVowels (int countval) {
    index;

    for (index = 0; index < countString; index++) {

    }
}

int countConsants (int countval) {
    index;

    for (index = 0; index < countString; index++) {

    }
}

int countVowAndCon (int countval) {
    index;

    for (index = 0; index < countString; index++) {

    }
}
Last edited on
You need to pass the pointers to your c-strings to your functions so that the functions could work with.

1
2
3
4
5
6
7
8
9
int countVowAndCon( char * my_c_string)
{
     int count = 0;
     for( int i = 0; my_c_string[i] != '\0'; ++i)
     {
        // check if my_c_string[i] is a vowel or a consonant
      }
      return count;
}


A c-string ends with a '\0' char
Last edited on
Can you simplify this a little for me? As far as what does the asterisk mean after the char?

And do I need to set up a char array for this program? And how would I do that if needed to?
The * means that what's passed into your function is a pointer to a char, not a char itself.

Presumably, you know that a C-string is implemented by an array of characters?

Presumably, you know that arrays are commonly passed into functions as pointers?
That asterisk means that the argument is a pointer, which points (generally spoken) to the address of some data. In this case to the c-string. You don't need to set a char array, because your string variables could easily get converted to such an array. To get a c-string from a std::string object, you can just use my_string.c_str() . This returns the c-string of a std::string object.

So you could pass the c_string at example by:
1
2
3
...
int count = countVowAndCon(my_string.c_str() )
...
Last edited on
Arrays in general: http://www.cplusplus.com/doc/tutorial/arrays/
Arrays of char: http://www.cplusplus.com/doc/tutorial/ntcs/
Pointers: http://www.cplusplus.com/doc/tutorial/pointers/

In case it is not said in them:
1
2
int foo( char gaz[] );
int bar( char* goo );

The bar and foo have identical parameter type.
The [] converts to * in the compiler.
The [] hints to the user that function expects an array (but you can call it with any pointer).


Consult the documentation: http://www.cplusplus.com/reference/string/string/c_str/
The return type of std::string::c_str is const char *.

Therefore,
int count = countVowAndCon(my_string.c_str() );
is possible only if pointer is to const char:
int countVowAndCon( const char * );
Last edited on
If you want to read in more than plain words into your strings, e.g. a whole sentence, it would be at best if you use std::getline. which enables you reading a whole line.

1
2
3
4
5
6
7
8
9
10
    ...
    string countString;
    string anthString;

    cout << "Please type in a sentence about anything and press <Enter>: " << endl;
    getline(cin, countString);

    cout << "Please enter another sentence and press <Enter>: " << endl;
    getline(cin, anthString);
    ...

It's generally a bad idea using std::cin for fetching user input, because it easily hangs up if the user inputs inexpected stuff.
Last edited on
Topic archived. No new replies allowed.