Frequency of characters entered.

Reply

Join Date: Oct 2006
Posts: 35
Reputation: paradoxxx is an unknown quantity at this point 
Solved Threads: 0
paradoxxx's Avatar
paradoxxx paradoxxx is offline Offline
Light Poster

Frequency of characters entered.

 
0
  #1
Oct 30th, 2006
Hi folks,

I have an array of chars letters[], it has both mixed letters, and symbols. I nned a code that keeps a count of each letter entered, and any a count for all other non-letter symbols entered.

Thanks in advance and God Bless.
Jonn.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Frequency of characters entered.

 
0
  #2
Oct 30th, 2006
Post your atempt.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Frequency of characters entered.

 
0
  #3
Oct 30th, 2006
there are only 255 possibilities, so just make an int array 255 and use the character as the index
  1. int count[255] = {0}
  2. char c = 'A';
  3. ++count[c];
when done the elemenets of count > 0 is the frequency you want.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: paradoxxx is an unknown quantity at this point 
Solved Threads: 0
paradoxxx's Avatar
paradoxxx paradoxxx is offline Offline
Light Poster

Here's my attempt.

 
0
  #4
Nov 1st, 2006
Hi All,

The program is supposed to uppercase the characters entered, transpose them (A to B, B to C ... Z to A). And also keep a count of all alphaitalics, how many A's, how many B's .... Z's. Also a count for all other non-alphaitalic characters entered. At the end output which alphaitalic occured the most.

I cannot figure out how the program to count the characters. The upper case transformation works, but does not work for z to Z.

Thanks.
JONN.



  1. #include <iostream>
  2. using namespace std;
  3. const char DECLARED_SIZE = 96;
  4. void uppercase(char letters[]);
  5. int main()
  6. {
  7. cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
  8. char letters[DECLARED_SIZE], next;
  9. int index = 0;
  10. int count = 0;
  11. for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
  12. index = 0;
  13. cin >> next;
  14.  
  15. while ((next != '#') && (index < DECLARED_SIZE))
  16. {
  17. letters[index] = next;
  18. index++;
  19. cin >> next;
  20. }
  21. letters[index] = next;
  22. int num_used = index;
  23. cout << "YOU ENTERED: \n";
  24. for (index = 0; index < num_used; index++)
  25. cout << letters[index] << " ";
  26. cout <<endl;
  27. uppercase(letters);
  28. return 0;
  29. }
  30. //CHANGE LOWERCASE TO UPPERCASE.
  31. void uppercase(char letters[])
  32. {
  33. int size = 0;
  34. int k = 0;
  35. char * answer;
  36. for (k = 0; letters[k] != '#'; k++)
  37. {
  38. size++;
  39. }
  40. answer = new char[size + 1];
  41. for (k = 0; k < size; k++)
  42. {
  43. if ((letters[k] < 122) && (letters[k] > 96))
  44. {
  45. answer[k] = letters[k] - 31;
  46.  
  47. if ((letters[k] < 90) && (letters[k] > 64))
  48. {
  49. answer[k] = letters[k] + 1;
  50.  
  51. if (letters[k] = 122)
  52. {
  53. answer[k] = letters[k] - 57;
  54.  
  55. if (letters[k] = 90)
  56. {
  57. answer[k] = letters[k] - 25;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. else
  64. {
  65. answer[k] = letters[k];
  66. }
  67. cout << answer[k] << " ";
  68. cout <<endl;
  69.  
  70. }
  71. return;
  72. }
Last edited by ~s.o.s~; Nov 1st, 2006 at 3:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Here's my attempt.

 
0
  #5
Nov 1st, 2006
Please use code tags when posting code. Its not all that difficult and makes your post look a whole lot better.

>>for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
This line is not necessary if you declare the array with an initializer, like this:
char letters[DECLARED_SIZE] = {0}, next;

Isn't function uppercase() supposed to convert the array to all upper case characters? Then why did you make it so difficult? It can be done in only a couple lines of code
  1. void uppercase(char letters[])
  2. {
  3. int k;
  4. for (k = 0; letters[k] != '#'; k++)
  5. letters[k] = toupper(letters[k]);
  6. }

I need a code that keeps a count of each letter entered, and any a count for all other non-letter symbols entered.
The code you posted does not do that.

You really do not need array letters at all, unless required by your instructor. what you need is an array of integers that keeps a count of the number of times a letter is entered. After you enter a character from the keyboard (or a file), convert the character to upper case then increment the array element (see my previous post for example).
Last edited by Ancient Dragon; Nov 1st, 2006 at 8:04 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Frequency of characters entered.

 
1
  #6
Nov 1st, 2006
Here's the code for a working solution i just made. What you do with the counters at the end is up to you though. Good luck!

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. // counter is an int array containing a counter for each possible character
  8. // entered. counter[0] is used for 'a', counter[1] for 'b', counter[27] for
  9. // 'A' and so on. counter[52] is used for all other symbols
  10. int counter[53] = {0};
  11.  
  12. // MAX_SIZE is the maximum number of characters to read from the screen
  13. const int MAX_SIZE = 80;
  14. // letters is the array containing the inputted string
  15. char letters[MAX_SIZE];
  16. cout << "Enter your string: ";
  17. cin.get(letters,MAX_SIZE);
  18.  
  19. // here we will check all characters from the inputted string and allocate the
  20. // counters in the counter array accordingly
  21. for (int i=0; i<MAX_SIZE; i++)
  22. {
  23. switch (letters[i])
  24. {
  25. case 'a': counter[0]++; break;
  26. case 'b': counter[1]++; break;
  27. case 'c': counter[2]++; break;
  28. case 'd': counter[3]++; break;
  29. case 'e': counter[4]++; break;
  30. case 'f': counter[5]++; break;
  31. case 'g': counter[6]++; break;
  32. case 'h': counter[7]++; break;
  33. case 'i': counter[8]++; break;
  34. case 'j': counter[9]++; break;
  35. case 'k': counter[10]++; break;
  36. case 'l': counter[11]++; break;
  37. case 'm': counter[12]++; break;
  38. case 'n': counter[13]++; break;
  39. case 'o': counter[14]++; break;
  40. case 'p': counter[15]++; break;
  41. case 'q': counter[16]++; break;
  42. case 'r': counter[17]++; break;
  43. case 's': counter[18]++; break;
  44. case 't': counter[19]++; break;
  45. case 'u': counter[20]++; break;
  46. case 'v': counter[21]++; break;
  47. case 'w': counter[22]++; break;
  48. case 'x': counter[23]++; break;
  49. case 'y': counter[24]++; break;
  50. case 'z': counter[25]++; break;
  51. case 'A': counter[26]++; break;
  52. case 'B': counter[27]++; break;
  53. case 'C': counter[28]++; break;
  54. case 'D': counter[29]++; break;
  55. case 'E': counter[30]++; break;
  56. case 'F': counter[31]++; break;
  57. case 'G': counter[32]++; break;
  58. case 'H': counter[33]++; break;
  59. case 'I': counter[34]++; break;
  60. case 'J': counter[35]++; break;
  61. case 'K': counter[36]++; break;
  62. case 'L': counter[37]++; break;
  63. case 'M': counter[38]++; break;
  64. case 'N': counter[39]++; break;
  65. case 'O': counter[40]++; break;
  66. case 'P': counter[41]++; break;
  67. case 'Q': counter[42]++; break;
  68. case 'R': counter[43]++; break;
  69. case 'S': counter[44]++; break;
  70. case 'T': counter[45]++; break;
  71. case 'U': counter[46]++; break;
  72. case 'V': counter[47]++; break;
  73. case 'W': counter[48]++; break;
  74. case 'X': counter[49]++; break;
  75. case 'Y': counter[50]++; break;
  76. case 'Z': counter[51]++; break;
  77. default : counter[52]++; break;
  78. }
  79. }
  80. return 0;
  81. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Frequency of characters entered.

 
0
  #7
Nov 1st, 2006
may4life -- it may be a somewhat working solution (there is at least one bug I can spot), but much too long and too difficult. It can be greatly simplified by using an array of 255 then using the letter entered as the index into the array. That eleminates that huge switch statement.
Last edited by Ancient Dragon; Nov 1st, 2006 at 8:09 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: paradoxxx is an unknown quantity at this point 
Solved Threads: 0
paradoxxx's Avatar
paradoxxx paradoxxx is offline Offline
Light Poster

Re: Frequency of characters entered.

 
0
  #8
Nov 1st, 2006
Thanks,
You guys are a great help. I was not supposed to use any library functions, that eliminated the uppercase function. Also the array was to have a maximum of 96 characters only.

JONN.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Frequency of characters entered.

 
0
  #9
Nov 1st, 2006
Originally Posted by paradoxxx View Post
Thanks,
You guys are a great help. I was not supposed to use any library functions, that eliminated the uppercase function. Also the array was to have a maximum of 96 characters only.

JONN.
The array of 96 characters is a limit on the number of characters you should enter from the keyboard. You need a another integer array to count the number of times a character was entered -- for example how may times did you enter the letter 'A', and how many times did you enter '1' ? Your program does not currently tell you that.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: paradoxxx is an unknown quantity at this point 
Solved Threads: 0
paradoxxx's Avatar
paradoxxx paradoxxx is offline Offline
Light Poster

Re: Frequency of characters entered.

 
0
  #10
Nov 1st, 2006
If I use this code:

int count[255] = {0}
char c = 'A';
++count[c];
How do I output the count of each character, what code do I use?
Last edited by paradoxxx; Nov 1st, 2006 at 8:33 am.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC