4 Replies - 18033 Views - Last Post: 11 November 2006 - 03:16 PM Rate Topic: -----

#1 manz2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-November 06

Number of characters in the string excluding spaces

Post icon  Posted 11 November 2006 - 01:11 PM

Hi,

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.
#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";
}


This post has been edited by Dark_Nexus: 11 November 2006 - 03:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Number of characters in the string excluding spaces

#2 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Number of characters in the string excluding spaces

Posted 11 November 2006 - 01:33 PM

Quote

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


Could you be more clear on these instructions?

a) Number of characters... is that string length? :blink:

c) Im assuming number of alphabets is number of letters... here is how you would do that:

				// c) Show number of letters in the string
				
				int x=0;
				for (int i=0;i<size;i++)
				{
					int temp=(int)chars[i];
					if ((temp>64 && temp<91) || (temp>96 && temp<123) x++;
				}
				cout << "The number of letters in the string is: " << x << endl;



Was This Post Helpful? 0
  • +
  • -

#3 manz2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-November 06

Re: Number of characters in the string excluding spaces

Posted 11 November 2006 - 02:10 PM

Thank you very much.

By looking at your code I can tell exactly how it works.

I think "Number of characters in the string excluding spaces " means any letters or punctuations without spaces.

and

"Number of alphabets in the string"

is just the way you did the coding.

All I have to do to the loop you made is:


int y=0;
for (int i=0;i<size;i++)
{
int temp=(int)chars[i];
if (temp>33 && temp<126)y++;
}
cout << "The number of characters in the string is: " << y << endl;

correct?

I tried it so far and seems to work.
:)

This post has been edited by manz2: 11 November 2006 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Number of characters in the string excluding spaces

Posted 11 November 2006 - 02:24 PM

Looks like it should work. :)

Might be quicker to simply search for spaces and substract them from total string length.
Was This Post Helpful? 0
  • +
  • -

#5 manz2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-November 06

Re: Number of characters in the string excluding spaces

Posted 11 November 2006 - 03:16 PM

Yep, I think that would be better, I changed the code to just
subtract total minus spaces and works better.

Thanks again!
:D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1