Solved

Need help with this small program that uses a string.

Posted on 2006-11-11
4
193 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fully specialized class template function 21 129
C Language combined operators 28 106
mixing C++ & C# in Vis Studio 2013 7 140
VS2015 compilation and missing DLLs 9 114
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

914 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

20 Experts available now in Live!

Get 1:1 Help Now