Counting the amount of letters in a phrase!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Counting the amount of letters in a phrase!

 
-1
  #1
Nov 12th, 2004
Hi guys,

Just wanted to ask you're opinions about this program I had to write for evening school, the idea is when you enter a phrase wich isn't longer then 80 letters, the programm will count the amount of each letter wich is written :!:

It doesn't have to keep account of 'a' or 'A', and I had to show each letter of the alphabet, just in case you we're wondering.

So, if you could just give me some pointers in wich way I could improve it, I would appreciate very much

// AantalLetters.cpp : Johan Berntzen: 11/11/2004

#include <stdafx.h>
#include <iostream.h>
#include <iomanip.h>

int zoek(const char s[], char ch);

void main(void)
{
int amount=0;
char str[80], letter='a';

cout<< " Write a phrase: ";
cin.getline(str,80);

for (int i=0;i<26;i++)
{
amount=zoek(str, letter);
cout<< letter << " = "<< amount;
cout<<setw(8);
if(i%7==6)cout<<endl;
letter++;
}
cin.get();
}

int zoek(const char s[], char ch)
{
short a=0;

for (int i=0;i<s[i];i++)
{
if (s[i]==ch)
a=++a;
}
return a;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Counting the amount of letters in a phrase!

 
0
  #2
Nov 12th, 2004
Before I make suggestions, I'll give you a cool version:
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <utility>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. map<char, int> get_frequency ( const string& s );
  10. void show ( const pair<char, int>& freq_item );
  11.  
  12. int main()
  13. {
  14. string s;
  15.  
  16. cout<<"Enter a string: ";
  17. if ( getline ( cin, s ) ) {
  18. map<char, int> freq = get_frequency ( s );
  19. for_each ( freq.begin(), freq.end(), show );
  20. }
  21. }
  22.  
  23. map<char, int> get_frequency ( const string& s )
  24. {
  25. string::const_iterator begin = s.begin();
  26. string::const_iterator end = s.end();
  27. map<char, int> freq;
  28.  
  29. for ( string::const_iterator it = begin; it != end; ++it )
  30. ++freq[*it];
  31.  
  32. return freq;
  33. }
  34.  
  35. void show ( const pair<char, int>& freq_item )
  36. {
  37. cout<< freq_item.first <<": "<< freq_item.second <<endl;
  38. }
On to the code!

>#include <stdafx.h>
This should be a avoided because it makes your code nonportable. To be more specific, you can only compile code using stdafx.h with Visual Studio AFAIK.

>#include <iostream.h>
>#include <iomanip.h>
These are old headers that technically aren't a part of C++. Remove the .h extension and you'll have the standard headers, but don'e forget to qualify standard names with std::.

>void main(void)
int main(). main never has, and probably never will return void. You can safely omit the void parameter because it means the same thing as an empty list in C++ and it's kind of ugly.

>letter++;
This relies too much on the character set. ASCII is not the only character set in existence, and some don't have the alphabet in sequential order. On such character sets your code will break.

The biggest problem with your code is that it's running time is quadratic. For each letter in the alphabet, you look over every letter in the string. For long strings this is a dreadful inefficiency and you can easily remove it by using a frequency table. My code uses the standard map class, which implements a frequency table through a binary search tree, but you can also use arrays with very little effort.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Counting the amount of letters in a phrase!

 
0
  #3
Nov 12th, 2004
Hi Narue,

Originally Posted by Narue
Before I make suggestions, I'll give you a cool version:
Thanks for the example, but you know, I prefere to try and write my own

On to the code!

>#include <stdafx.h>
This should be a avoided because it makes your code nonportable. To be more specific, you can only compile code using stdafx.h with Visual Studio AFAIK.
Thanks for the tip, I'll keep that in mind :!:

>#include <iostream.h>
>#include <iomanip.h>
These are old headers that technically aren't a part of C++. Remove the .h extension and you'll have the standard headers, but don'e forget to qualify standard names with std::.
Now that is strange, we're being teached this way of programming and you say that it's the old way, can you explain this a bit more

>void main(void)
int main(). main never has, and probably never will return void. You can safely omit the void parameter because it means the same thing as an empty list in C++ and it's kind of ugly.
So, if I understand correctly, it's better to start writing main with 'int main()' can you explain why because I'm not asking a return from main

>letter++;
This relies too much on the character set. ASCII is not the only character set in existence, and some don't have the alphabet in sequential order. On such character sets your code will break.
Well, we we're told to use ASCII, so :o I'll have to do it with ASCII, I understand that in certain circumstances it is better to use another way. But for this program, do you think it is good enough

The biggest problem with your code is that it's running time is quadratic. For each letter in the alphabet, you look over every letter in the string. For long strings this is a dreadful inefficiency and you can easily remove it by using a frequency table. My code uses the standard map class, which implements a frequency table through a binary search tree, but you can also use arrays with very little effort.
Well, that's my biggest problem aswell, in my code I have to use an array of 26 integers wich represent 0 to 25 and each number stands for one letter, problem is, I don't know how to solve that problem :rolleyes:

Also, is there a way that I can use my code, but altered in a manner that it doesn't have to go trough the loop so many times

To me it seems that now, the string allways has to go along with the letters each time it has to be checked :!: How could I change it in a manner that during the control of the letters the string stays put untill the total control of all numbers is done
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Counting the amount of letters in a phrase!

 
0
  #4
Nov 13th, 2004
Now that is strange, we're being teached this way of programming and you say that it's the old way, can you explain this a bit more
It's been that way since the introduction of C in the 1970s and only (relatively) recently been replaced (2000 or so) by the new system as default.
Many books still use the old system only, as do older compilers.
And newer compilers still accept the old way as an alternative to remain compatible with old code. Many teachers just haven't caught on yet (if they even know, many teachers create their course at some point and never bother to keep it (and their own knowledge) up to date after that.

So, if I understand correctly, it's better to start writing main with 'int main()' can you explain why because I'm not asking a return from main
Because the language standard asks for it would be a good enough reason. It's not strictly required I think (the compiler won't complain after all) but it's good practice.
Every program needs to have a return code, with 0 meaning (on most operating systems) there was no error.

Well, we we're told to use ASCII, so I'll have to do it with ASCII, I understand that in certain circumstances it is better to use another way. But for this program, do you think it is good enough
Always code for the future. In real projects you'd be told to code for ASCII and halfway through some analyst would storm into your office and tell you they have landed a new customer in Turkey or Malaysia who wants a localised version which means you'll have to be testing UTF-8. Better to be prepared for that from the outset, especially if it's an easy thing to do.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Counting the amount of letters in a phrase!

 
0
  #5
Nov 13th, 2004
@ jwenting, thanks for the explanation, I have no doubt that what I wrote would be unusable in other examples, but fact is, I have to use ASCII :o

It's our assignement :!:

@ Narue, am I correct when I say that your code is written in OO

Because this: map<char, int> get_frequency
and this: string::const_iterator begin = s.begin(); type of code, we haven't seen in structured programming.

We are going to start Object Oriented in one or two weeks :!:

Oh yes, going to order following books by the way:

- 3D Math Primer for Graphics and Game Development.
ISBN: 1556229119
- Essential Mathematics for Computer Graphics
ISBN: 1852333804
- Concrete Mathematics: A Foundation for Computer Science
ISBN: 0201558025
- Art of Computer Programming Volumes 1-3 Box set
ISBN: 0201558025

What do you think
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Counting the amount of letters in a phrase!

 
0
  #6
Nov 13th, 2004
Do you have the bible?
The C++ Programming Language 3rd edition (Stroustrup)? That's pretty much required fodder for everyone who's into C++.
I don't know any of the books you mention, but the titles sound like fun

I've ordered "C ++ for Game Programmers" last week which should arrive in a few days, got good reviews at gamedev.net. Some reviewers on Amazon complained there's not enough DX and graphics specific code in there but IMO that's a good thing as there's tons of books about that. Instead it focusses on things like performance and other generic stuff with applications in (among other things) games.

If you want a good intro on 3D mathematics on a 2D screen I can recommend "Flights of Fantasy" by Christopher Lampton. It's certainly been out of print for over a decade though, it still deals with DOS and 640x480x256 resolution but the explanations of the math involved are good. ISBN 1878739182 Waite Group, 1993. Maybe a second-hand bookstore or library clearout sale can turn it up.
I've also learned quite a bit from another oldie, a book on writing graphics drivers for DOS in Assembly and C (and in fact I've once written a partial functionality driver for Turbo Pascal, sadly never completed as the project was cancelled)...

For the rest I'm afraid most of my recent (say last 5 years) library has been Java books as that's what I mainly use professionally.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Counting the amount of letters in a phrase!

 
0
  #7
Nov 13th, 2004
>but you know, I prefere to try and write my own
That's why I showed you an example that couldn't possibly be accepted as homework. It's just a way for me to show off.

>we're being teached this way of programming and you say that it's the old way, can you explain this a bit more
Before C++ was standardized, everyone basically agreed to use headers such as iostream.h as defined by The Annotated C++ Reference Manual written by Bjarne Stroustrup. When the language was standardized in 1998, these headers were edited heavily and the names were changed to remove the .h extension. You're being taught an old dialect of C++ that is quickly dying out.

>can you explain why because I'm not asking a return from main
If your compiler doesn't handle standard C++ then not returning a value from main results in undefined behavior. Undefined behavior is one of the worst things that your programs can have because it means they can do anything they like including (but not limited to) destructive operations on your computer such as wiping out data. Standard C++ defines main to return 0 (for success) if execution falls off the end, so you can omit a return statement safely.

However, just because you don't return a value explicitly doesn't mean that you can change the definition of main, which the standard states shall be (and when they use the word shall, you'd better do it or suffer undefined behavior):
  1. int main()
  2.  
  3. or
  4.  
  5. int main ( int argc, char *argv[] )
  6.  
  7. or anything equivalent
Telling main to return void is undefined behavior, but in practice it's entirely possible that the program would simply fail to run if the run-time startup code sees a different call/return than it expected. Sadly, several systems support void main as an extension, but keep in mind that it's only an extension. Your code is not portable.

>Well, we we're told to use ASCII
You have to do what you have to do. Just remember that by assuming ASCII your code isn't as portable as it could be.

>I don't know how to solve that problem
ASCII letters are in sequential order. You can take advantage of that to create an array of 26 rather than an array of 128:
  1. #include <cctype>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char letters[] = "abcdefghijklmnopqrstuvwxyz";
  9. int freq[sizeof letters - 1] = {0};
  10. char str[80];
  11.  
  12. cout<<"Enter a string: ";
  13. if ( cin.getline ( str, sizeof str ) ) {
  14. for ( int i = 0; str[i] != '\0'; i++ ) {
  15. if ( isalpha ( str[i] ) )
  16. ++freq[tolower ( str[i] ) - 'a'];
  17. }
  18.  
  19. for ( int i = 0; letters[i] != '\0'; i++ ) {
  20. if ( freq[i] != 0 )
  21. cout<< letters[i] <<": "<< freq[i] <<endl;
  22. }
  23. }
  24. }
Otherwise you would need to account for every character in the character set, then discard what you don't want manually:
  1. #include <cctype>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int freq[128] = {0};
  9. char str[80];
  10.  
  11. cout<<"Enter a string: ";
  12. if ( cin.getline ( str, sizeof str ) ) {
  13. for ( int i = 0; str[i] != '\0'; i++ ) {
  14. if ( isalpha ( str[i] ) )
  15. ++freq[tolower ( str[i] )];
  16. }
  17.  
  18. for ( int i = 0; i < 128; i++ ) {
  19. if ( isalpha ( static_cast<unsigned char> ( i ) ) && freq[i] != 0 )
  20. cout<< static_cast<unsigned char> ( i ) <<": "<< freq[i] <<endl;
  21. }
  22. }
  23. }
Neither is better than the other simply because the former is more efficient for your purposes, but the latter is efficient if you need a more general frequency table for the character set.

>And newer compilers still accept the old way as an alternative to remain compatible with old code.
This is changing though. For example, Visual Studio .NET fails to compile prestandard C++.

>It's not strictly required I think (the compiler won't complain after all)
It is strictly required, and a compiler that doesn't complain is probably set to allow non-standard extensions and void main is one of them. But when set to disable extensions, all decent compilers should flag a warning.

>with 0 meaning (on most operating systems) there was no error
0 is success for all implementations, on all systems, or it's not C++.

>Narue, am I correct when I say that your code is written in OO?
Yes and no. I make use of standard objects, but the term generic programming (through templates) would be more appropriate than object oriented programming in this case.

>- Art of Computer Programming Volumes 1-3 Box set
Good choice. These are among my favorites. They're essential for any professional or hardcore hobbyist.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Counting the amount of letters in a phrase!

 
0
  #8
Nov 13th, 2004
>That's why I showed you an example that couldn't possibly be accepted as homework. It's just a way for me to show off.

<<Oh, showboat LOL

>Before C++ was standardized, everyone basically agreed to use headers such as iostream.h as defined by The Annotated C++ Reference Manual written by Bjarne Stroustrup. When the language was standardized in 1998, these headers were edited heavily and the names were changed to remove the .h extension. You're being taught an old dialect of C++ that is quickly dying out.

<<Understood, I'll certainl mention this to him

>...returning a value from main results in undefined behavior.

<<Understood.

>You have to do what you have to do. Just remember that by assuming ASCII your code isn't as portable as it could be.

<<Well, for this excercise I'll use it, but will keep in mind of the portability problem with it!

>Neither is better than the other simply because the former is more efficient for your purposes, but the latter is efficient if you need a more general frequency table for the character set.

<<Thanks for the examples, In recoding my own, I'll surely will try and see how and more important why you did it in that certain way :!:

>Yes and no. I make use of standard objects, but the term generic programming (through templates) would be more appropriate than object oriented programming in this case.

<<Well, I'll try that type of coding out within a few months(years) :lol:

>Good choice. These are among my favorites.

<< Well, you lead me to them

@jwenting, nope haven't got Stroustrup, I saw that it's last print was from 2000, little bit old don't you think?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Counting the amount of letters in a phrase!

 
0
  #9
Nov 13th, 2004
>little bit old don't you think?
IMO, the only better reference than The C++ Programming Language, is the C++ standard itself. Rest assured that everything in it is up to date enough for your needs.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Counting the amount of letters in a phrase!

 
0
  #10
Nov 14th, 2004
Originally Posted by Narue
>little bit old don't you think?
IMO, the only better reference than The C++ Programming Language, is the C++ standard itself. Rest assured that everything in it is up to date enough for your needs.
Ok, I'll keep it in mind :!:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC