Solved

Need help with this small program that uses a string.

Posted on 2006-11-11
4
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17922381
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
ID: 17922391
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
ID: 17922435
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

739 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