Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with this small program that uses a string.

Posted on 2006-11-11
4
Medium Priority
?
201 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

636 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