Solved

Need help with this small program that uses a string.

Posted on 2006-11-11
4
192 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:manz2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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];
}
0
 

Author Comment

by:manz2
Comment Utility
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";
}



0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Well, that's a good start. For 'a.' and 'c.' you just have to introduce the suitable tests, i.e.

if (ch != ' ') number_of_chars_excluding_spaces++;

or

if (0 != isalpha(ch)) number_of_alphabets++;
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now