Need help (Counting Instances of letters )

Oct 17, 2015 at 2:55am
I have a problem, We are not allowed to use ARRAYS:
Heres a sample output:

How many iterations do you want?: 5
Enter a letter: a
Enter a letter: z
Enter a letter: z
Enter a letter: A
Enter a letter:b

You keyed in lowercase 'a': 1 time(s)
You keyed in lowercase 'b' : 1 time(s)
You keyed in lowercase 'z' : 2 time(s)
You keyed in uppercase 'A' : 1 time(s)

I can only use loops and if...else
Last edited on Oct 17, 2015 at 3:19am
Oct 17, 2015 at 2:58am
your question is ?
Oct 17, 2015 at 3:19am
Make a program that asks a user how many letters to be entered, then accepts any letter upper and lower cases then displays how many times the certain letter is typed.
Oct 17, 2015 at 3:39am
That's still not a question, but does seem to be a request for someone to write it for you, using those guidelines. Sorry, but we only help with programs you're writing, pointing you in the right direction to get your creation working. We just help you learn.
Oct 17, 2015 at 4:36am
#include<iostream>
using namespace std;

main()
{
int limit,a=0,b=0,A=0,B=0;
char letter;
cout<<"How many iterations do you want?" ;
cin>>limit;

for (int init =1; limit>=init;init++)
{
cout<<"Enter a letter :" ;
cin>>letter;

if (letter=='a')
{
a++;
}
else if (letter=='b')
{
b++;
}
else if (letter=='A')
{
A++;
}
else if (letter=='B')
{
B++;
}

}
if (a>0)
{
cout<<"You keyed in lowercase 'a' :"<<a<<"Time(s)"<<endl;
}
if (b>0)
{
cout<<"You keyed in lowercase 'b' :"<<b<<"Time(s)"<<endl;
}
if (A>0)
{
cout<<"You keyed in uppercase 'A' :"<<A<<"Time(s)"<<endl;
}
if (B>0)
{
cout<<"You keyed in uppercase 'B' :"<<B<<"Time(s)"<<endl;
}
return 0;
}


This is the only idea i have but my instructor says there is a shorter code for this but i am not allowed to use array. I just wanna know the process.-
Last edited on Oct 17, 2015 at 4:39am
Oct 17, 2015 at 5:05am
@servers09

Your for loop won't go anywhere.

for (int init =1; limit>=init;init++)

should be..
for (int init = 0; init < limit;init++)

And I'm guessing your code is just for checking if things work, as you're only checking 2 different letters right now, lower and caps
Oct 17, 2015 at 6:27am
I just wanna know if there is another way to shorten the code instead of typing from a to z, one by one (if...else)
Last edited on Oct 17, 2015 at 6:30am
Oct 17, 2015 at 11:23pm
Can anyone help?
Oct 18, 2015 at 1:02am
Creating a dynamic array. When your code read a new character and save it then count++ or a saved character just count++. You should write a function to check its duplicate or not.
Oct 18, 2015 at 1:26am
I cant use array tho
Oct 18, 2015 at 1:39am
@servers09

How about a vector? Can you use those?
Oct 18, 2015 at 1:56am
Nope, only loops and if..else
Oct 18, 2015 at 2:55am
@servers09

Here is my version for your program. It doesn't use arrays or vectors. Please don't try turning this in as your works, but do study it, and figure out how, and why, it works. If you have questions about the program, please ask me. I'll do my best to explain..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include <string>

using namespace std;

int main()
{
	int limit, character;
	string letter="";
	char next;
	cout << "How many iterations do you want?";
	cin >> limit;

	for (int init = 0; init < limit; init++)
	{
		cout << "Enter a letter : ";
		cin >> next;
		letter += next;
	}

	for (int alphabet = 65; alphabet < 91; alphabet++) // Capital letters in alphabet
	{
		character = 0; // Reset count for next letter
		for (int init = 0; init < limit; init++) // Run through each character in string input
		{
			if (letter[init] == char(alphabet)) // Compare the two
				character++; // Increase if a match
		}
		if (character > 0)
			cout << "You keyed in uppercase '" << char(alphabet) << "' : " << character << " Time(s)." << endl;
	}
	for (int alphabet = 97; alphabet < 123; alphabet++) // Lowercase letters in alphabet
	{
		character = 0;
		for (int init = 0; init < limit; init++)
		{
			if (letter[init] == char(alphabet))
				character++;
		}
		if (character > 0)
			cout << "You keyed in lowercase '" << char(alphabet) << "' : " << character << " Time(s)." << endl;
	}

	cin >> next; // Prevent screen from closing when program finishes
	return 0;
}
Oct 18, 2015 at 4:23am
Thanks, yes i wont turn this in , but what does the string letter=""; mean?
Oct 18, 2015 at 4:41am
@servers09

string letter=""; is the string variable that's going to hold all your char inputs. After being asked how many iterations, or inputs, your going to do, a for loop is created with as many as you entered. Each character you input, is added to the end of the string called letter. They're added in with the letter+=next, which just means, letter equals letter plus the variable called next When finished, you'll have one string with all the inputted letters. In case you're wondering about, for (int alphabet = 65; alphabet < 91; alphabet++). char(65) equals 'A', char(66) is 'B', and so on. So, we're checking if char(65) is equal to each of the inputted letters in the string letter. If yes, the variable, character, is increased by one. At the end of searching your string length, the variable character amount is printed, character is set back to 0, and the complete string is now searched for 'B'. etc.
Last edited on Oct 18, 2015 at 5:07am
Topic archived. No new replies allowed.