Hi there.

I'm fairly new at using C++ and I have to do program on this summer course I'm doing. Trouble is, despite what I have read through and been taught, I just can't get my head around a few things regarding this program I need to make within three days.

Here's the breif I have:

Write a program that asks the user to input a word, or series of characters. The program should determine the length of the users character string, and count the number of occurrences of each letter. This information should then be output to the screen.
The program should store the user input in an appropriate structure before determining length and counting occurrences of each letter.


And here's the code I've come up with so far:

using namespace std;

#include <iostream>
#include <string>

int main()
{
    string userwd;
    
    cout << "Hello.\nPlease input a word to analyse: ";
    cin >> userwd;
    cout << "Your word is: " << userwd << ".\n";
    int letters;
    count userwd;
    int count[letters];
    system("PAUSE");

return 0;
}

I know it might seem stupid, but I'm not a logical thinker. If there's any help you can offer, it'll be greatly appreciated.

Many thanks,

Dr Stupid

Length of a string can be found using _____.length(); {Fill the space with the name of the variable}

You can then use a for loop to check every single character inside the string and compare it with one another to see if it repeats. While you are doing this, you need to keep track of which letter you have already checked for. (to avoid repeating the same characters when they appear again).

Basically, I THINK this problem can be solved using a simple for loop. I will edit this post and post my solution when I get the VC++ installed on my computer.

EDIT :: Or, you can check for every single letter of the alphabet and display 0 if they don't appear at all. Like check the entire string for 'a' then 'b' and count how many you find and then display the number BEFORE moving on to the next letter. You can also put a if statement that can block any letter with 0 occurance from displaying.

E.G, you check for 'a' and incremement the variable count by one everytime you find 'a'. If you finish counting and the value of count is still 0 then you put a if statement.

if (!(count == 0))
{
display the character's occurance information
}

Thanks for the assist.

Still not too sure about displaying the number each letter occurs, but here's a new post of my cleaned up code:

using namespace std;

//include ability to input/output information
#include <iostream>
//include ability to manipulate strings
#include <string>

int main()
{

//create the users 'word' variable
    string userwd;
//prompt iser for input
    cout << "Hello ^v^\n\nPlease input a word to analyse: ";
//set unput to users 'word' variable
    cin >> userwd;
//display users word
    cout << "\nYour word is: " << userwd << "\n\n";
//count the length of the users word and put it in a variable
    int length = userwd.length();
//display the users word and the length of it as an integer    
   cout << "The length of " << userwd << " is " << length << ".\n\n";
 
//prevent program from exiting before the user can see any information
    system("PAUSE");

return 0;
}

Looks better and runs for me fine so far.

"counting occurrences of each letter." Does that mean you count
all letters from a to z? In a sentence and display all letters and the
number of occurrence?

Is this your first assignment???

Letters A to Z, a to z
You'll need an array to tally 26 letters. Preclear it! A loop will work.
You need a string length of your buffer.
You need to look at each character to get tally index.

index = (int)( toupper( userwd[ i ] ) - 'A' );

Then print the result when done with parsing the string.

Thanks for the assist.

Still not too sure about displaying the number each letter occurs, but here's a new post of my cleaned up code:

The problem you posted says that you need to display how many time a letter of the alphabet occures in the input. Here is a possible but incomplete solution....

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string userword;
	int count;

	cin >> userword; // Read the input from user (Use getline(cin, userword); if you are expecting a sentence with spaces)

	cout << "The word '" << userword << "' is " << userword.length() << " characters long." << endl; // Obvious...

	cout << "\n\nLower Character Occurances are :" << endl;

	for (char i = 'a'; i <= 'z'; i++) // Insted of using integers to loop through this for loop, I am using 
	{								  // characters so I don't have to do unnecessary type casting and all that complicated stuff....
		count = 0;

		for(unsigned int ii = 0; ii <= userword.length(); ii++) // This for loop is going through all the characters in the string...
		{
			if(i == userword[ii]) // checks if i occures anytime in the entire string, if it does, it will increment count by 1.
			{
				count++;
			}
		} // for loops ends here...

		if(! (count == 0)) // If it is 0, there is no point in displaying this information...
		{
			cout << i << " : " << count << endl; // kinda obvious...
		}
	}

	cout << "\n\nCapital Character Occurances are :" << endl; // Same thing but this time it checks for capital letters...

	for (char i = 'A'; i <= 'Z'; i++)
	{
		// This gotta be easy now!!!
	}

	return 0;
}

dj, thats one semi-correct way of doing it, another approach is do
something of the following :

std::string lowStr = "abcdefghijklmnopqrstuvwxyz";
int lowCnt[26] = {0};
std::input;
cin >> input;

for(int i = 0; i < input.length(); i++)
{
   //something similar to this
   if(input[i] == lower[ int ( (input[i]) - 'a' ) ] //this is not even needed. Just a visual reminder
     lowCnt[i] +=1;

  //Alternative to above you can do just this, realizing that elem 0 is 'a' elem1 is 'b' and so on
    lowCnt[ int( input[i] - 'a') ] +=1
}

//Display result...

First Person: I was just trying to keep it simple because 'Dr.Stupid' (I feel like I am insulting him but I am not...) is a beginer. So yeah, if I had to do something like this, I would have probabaly thought of something different like first converting everything into lower or uppercase and then do some random thing with it...


Wildgoose: I didn't intentionally put errors but I did leave the capital part for him to figure out. When I compile it, it shows me no warnings or errors.

Just curious, what errors are there? Are you talking about the single quote within the double quotes? Or using char i twice?

"First Person: I was just trying to keep it simple because 'Dr.Stupid' (I feel like I am insulting him but I am not...) is a beginer. So yeah, if I had to do something like this, I would have probabaly thought of something different like first converting everything into lower or uppercase and then do some random thing with it...
"

It's nice to start thinking of different ways to solve a problem
especially when you first start.

I am not sure if you still are monitoring this thread, but when you use <= with ____.length();. It loop one more time. This is caused because I start at 0 and go upto and including the total input character. (Pretty sure that doesn;t make any sense...)

Example:

If the length was 3 then,

it will first loop as 0 then 1 then 2 then 3. In total, thats a total of four times...NOT a bug for this problem but if the use was different it may give you trouble....

This article has been dead for over six months. Start a new discussion instead.