C++ counting letters

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 580 Views - Last Post: 01 February 2013 - 12:20 PM Rate Topic: -----

#1 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

C++ counting letters

Posted 30 January 2013 - 04:40 PM

I have a "simple" code to write: take a string and count the letters.
Clearly missing basic step, but have tried everything I can find in my book and DIC tutorials, for the last 3 hours.
Added a "while" to the double "for" loops for control without success.
With it this way, I get "a: 1 times" constantly. Any advice would be appreciated.
#include <string>
#include <iostream>
using namespace std;

int main()
{
string sentence;
cout<<"Enter sentence: ";
getline(cin, sentence, '\n');
int counts[100];
const char s[] = {'a','b','c'.......};

count(s, counts, sentence);
return 0;
}
void count(const char s[], int counts[], string sentence){
int count = 0;
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
if(sentence[i] == s[j]){
count++;
}
if (count == 0){
count = 0;
}
else{
cout<<s[i]<<":"<<count<<" times"<<endl;
}
}
return;
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ counting letters

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C++ counting letters

Posted 30 January 2013 - 06:04 PM

The first thing to correct is that you shouldn't loop 100*100 times, going beyond the size of the array and string. You need to limit these to (one less than) the sizeof the array/string.

This
if (count == 0){
    count = 0;
}

doesn't do anything.

It would be neater if you looped twice:
once to calculate the results, and a second time to print them (for each letter of the alphabet).

BTW Your code is very hard to read without proper indentation.

This post has been edited by andrewsw: 30 January 2013 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: C++ counting letters

Posted 30 January 2013 - 07:11 PM

This problem is better done using a frequency table. Since ascii characters take values from 0 - 127, you can use an array of a 127 integers as a frequency table. Loop through every character in your input array and increment the frequency table at that character value.

i.e.

#define ASCII_MAX 127

int freqTable[127];
memset(freqTable, 0, sizeof(int) * ASCII_MAX);

freqTable['a']++;
freqTable['b']++;
freqTable['b']++;

//freq table contains
// a - 1
// b - 2
// other - 0



Was This Post Helpful? 0
  • +
  • -

#4 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 07:22 AM

Thanks, will change the loops.
When I write the loops :
 for(int i =0; i < sizeof(sentence)-1; i++){
          if(sentence.at(i) == s[0]){

My teacher wants me to use const char s[] to store the letter occurrence count. Will
 count[chars[i]]++;

allow me to do the count?
If so how do I use it with const?
Was This Post Helpful? 0
  • +
  • -

#5 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 07:36 AM

Sorry the times of the occurrence is stored in counts[] not at const. I had it right the 1st time, but still confused on how to increment the count and then reset to 0 for the next letter in the two loops. In Java I would use one loop to go through the sentence and a nested for loop to go through the letters and have one if to compare. Is it the same in C++?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,543
  • Joined: 05-May 12

Re: C++ counting letters

Posted 31 January 2013 - 07:39 AM

Yes, the concept in Java would apply equally as well in C++.
Was This Post Helpful? 0
  • +
  • -

#7 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 09:40 AM

I think I cleaned it up some, but still getting ASCII values for s[i] and location number for count[i] in output. Where did I get turned around?
#include <cstring>
#include <string>
#include <iostream>
using namespace std;

void count(const char s[], int counts[], string sentence);
int main()
{
	string sentence;
	cout<<"Enter a string: ";
	getline(cin, sentence);
	signed int counts[25];
	const char s[]={'a','b','c','d','e', 'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 
	
	count(s, counts, sentence);

	return 0;
}

void count(const char s[], int counts[], string sentence)
{
		for(signed int i = 0; i < sizeof(sentence)-1; i++){
			for(signed int j = 0; j < sizeof(s)-1; j++){
				if(s[j] == sentence.at(i)){
					counts[i]++;
			}
		   if(counts[i] != 0){
				cout<<s[j]<<": "<<counts[i]<<" times"<<endl;
			}
			}
			return;
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: C++ counting letters

Posted 31 January 2013 - 10:51 AM

Hi,

You are not intialising

signed int counts[25];

this would do

signed int counts[25]={0};

also standard string has a length function

and also a size function

so you could just do this

for(unsigned int i = 0; i < sentence.length(); i++)

for your second loop.

I would define something like
#define MAX_LETTERS 25

and then use that in your second loop
for(unsigned int j = 0; j < MAX_LETTERS; j++)

you can use MAX_LETTERS instead of the number 25 in the rest of the code.

you are also confusing your i's with your j's

it should be inside your nested loop

if(s[j] == sentence.at(i))
			{
				counts[j]++;
			}
		





also move the printing out of the function and do that in your main

so inside main at the end you would have

something like

for(int i=0; i<25; i++)
cout<<s[i]<<": "<<counts[i]<<" times"<<endl;



Snoopy.

This post has been edited by snoopy11: 31 January 2013 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#9 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 11:34 AM

Made the changes:
for(signed int k = 0; k < 26; k++){
		 if(counts[k] != 0){
				cout<<s[k]<<": "<<counts[k]<<" times"<<endl;
						}


because I am not to display letters that do not appear in the sentence.
Still getting (z: -858993460 times) as an answer.
Tried to clean up the function:
void count(const char s[], int counts[], string sentence)
{
	for(signed int i = 0; i < sentence.length()-1; i++){
			for(signed int j = 0; j < sizeof(s)-1; j++){
				if(sentence.at(i) == s[j]){
					counts[j]++;
				}
			}
		  return;
	}
}

But am unfamiliar with #define and it's uses. I have only been using c++ for 3 weeks, and appreciate all your advice.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: C++ counting letters

Posted 31 January 2013 - 01:14 PM

There are 26 letters in the alphabet.

    signed int counts[25];



You can find info on arrays here. For #define look at preprocessor directives near the end of the tutorial.
C++ Language Tutorial


You could try printing the value sizeof(s), in the function, to check it is correct.

cout << "sizeof(s)=" << sizeof(s) << endl;


Was This Post Helpful? 0
  • +
  • -

#11 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 01:38 PM

Was counting 0-25 instead of using English 1-26. sorry.
Will keep reading.
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: C++ counting letters

Posted 31 January 2013 - 01:41 PM

I'm not sure I made myself clear, this creates an array with 25 elements.

	signed int counts[25];


Was This Post Helpful? 0
  • +
  • -

#13 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 02:13 PM

I understand and fixed that earlier today. I have been studying this and another program all day to understand how to get the user input to be read and inserted into an array and a 2D array.
Tried cin>>, getline(cin, sentence, '\n'), cin.getline(), and anything else I come across in the DIC tutorials, my class book, cplusplus.com, cprogramming.com.
Anywhere else I should look?
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: C++ counting letters

Posted 31 January 2013 - 02:24 PM

The variable/object sentence is not an array, string is a class.

Can you give a coded exapmle of what you are trying to do?
Was This Post Helpful? 0
  • +
  • -

#15 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: C++ counting letters

Posted 31 January 2013 - 02:38 PM

Tried to get the string to an array without luck:
#include <cstring>
#include <string>
#include <iostream>
using namespace std;

void count(const char s[], int counts[]);
int main()
{
	
	signed int counts[26] = {0};
	const char s[]={'a','b','c','d','e', 'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 
	
	count(s, counts);

	 for(signed int k = 0; k < 26; k++){
		 if(counts[k] != 0){
				cout<<s[k]<<": "<<counts[k]<<" times"<<endl;
						}
	 }
	return 0;
}

void count(const char s[], int counts[])
{
	string sentence;
	cout<<"Enter a string: ";
	getline(cin, sentence);
	
	for(signed int i = 0; i < sentence.length(); i++){
	
			for(signed int j = 0; j < strlen(s); j++){
					if(sentence.at(i) == s[j]){
					counts[j]++;
				}
			}
		  return;
	}
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2