Link to home
Start Free TrialLog in
Avatar of manz2
manz2

asked on

Need help with this small program that uses a string.

Hi Experts,

I'm writing this program, my instructions are:

Write function that accepts a string as an input then prints out the following:
a. Number of characters in the string excluding spaces
b. Number of letter "a" (small or capital) in the string
c. Number of alphabets in the string

I would like to know if as right now my program is coded as directed, and if you could please
let me know how I would go about doing a and c.

I have spent a big portion of the day trying to lookup examples or info on the net or on this site on how that is done
with no success.

Thank you for your time and help.
Avatar of jkr
jkr
Flag of Germany image

Well, since this is homework, it would be a good idea to post what you already have. And as 'b.' does not seem to be a problem, why don't you use the same techique for 'a.' and 'c.', because the task is almost identical. As a hint, use a loop to iterate through the string like

unsigned int len = strlen(str);

for (unsigned int i = 0; i < len; ++i) {

    char c = str[i];
}
Avatar of manz2
manz2

ASKER

Sorry, I forgot to post my program.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void showValues(string, int);

int main()
{
      string sentence;

      cout << "Enter any sentence you wish and I will tell you the following:\n" <<  endl;
      cout << "1. Number of letter A's (small or capital) in the string.\n";
      cout << "2. Number of characters in the string excluding spaces.\n";
      cout << "3. Number of alphabets in the string.\n" << endl;
      cout << "Enter sentence now:\n";
      getline(cin, sentence);

      showValues(sentence, sentence.length());
      system("pause");
      return 0;
}

void showValues (string chars, int size)
{
      cout << "\nThe string is:"<< endl;
      for (int index = 0; index < size; index++)
            cout << chars[index];
      cout << endl;

      // Show how many A's

      char ch;
      int vowelCount = 0;

      for (int pos = 0; pos < size; pos++)
      {
            ch=toupper(chars[pos]);

            switch(ch)
            {
            case 'A': vowelCount++;
            }
      }
      cout << "\nThere are " << vowelCount << " A's (small or capital) in the string.\n";
}



ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial