Homework:

Thread Solved

Join Date: Sep 2009
Posts: 5
Reputation: terzenta is an unknown quantity at this point 
Solved Threads: 0
terzenta terzenta is offline Offline
Newbie Poster

Homework:

 
0
  #1
Sep 20th, 2009
(Most frequent character in a string)

I've been working on this problem for hours, and I'm just totally lost. I don't know if I'm heading in the right direction at all, and would really appreciate any help. My code isn't working at all, but I've included it.

The homework problem is this:
Write a function that accepts either a pointer to a C-string or a string object, as its argument. The function should return the character that appears most frequently in the string. Demonstrate the function in a complete program.

  1. //This program takes in a pointer to a c-string and returns the character that appears most frequently
  2. #include <iostream>
  3. #include<string>
  4. #include<cstring>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. int letterIndex, stringCounter, highestNumber, highestLetter;
  11. int counter[80];
  12. char *letter[] = {"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"};
  13. const int length = 80; // Maximum length for string
  14. char line[length]; // Array of char
  15.  
  16. int numberOfLetters = 0, index = 0;
  17. letterIndex = 0;
  18. stringCounter = 0;
  19. highestNumber = 0;
  20.  
  21. // Read a string into the character array.
  22. cout << "Enter in a word or sentence less than "
  23. << length-1 << " characters:\n";
  24. cin.getline(line, length);
  25. numberOfLetters = strlen(line);
  26. while (letterIndex < 26) {counter[letterIndex] = 0;
  27. letterIndex++;}
  28.  
  29. while (letterIndex <= 25) {
  30. while (stringCounter <= numberOfLetters){
  31. cout << letter[letterIndex];
  32. if (letter[letterIndex] == line[stringCounter]) {cout << "it worked";}
  33. //counter[letterIndex]++;}
  34. stringCounter++;}
  35.  
  36. letterIndex++;}
  37.  
  38. letterIndex = 0;
  39.  
  40. while (letterIndex < 26) {cout << counter[letterIndex] << endl;
  41. letterIndex++;}
  42.  
  43. letterIndex = 0;
  44.  
  45. //while (letterIndex <= 25) {
  46. // if (counter[letterIndex] > highestNumber) {
  47. // highestNumber = counter[letterIndex];
  48. // highestLetter = letterIndex;}
  49. // letterIndex++;
  50. // }
  51.  
  52. cout << "The letter you used most frequently was: " ;//<< letter[highestLetter] << ".\n";
  53.  
  54. return 0;
  55. }

Like I said, I just have no idea where to go. Any help would be appreciated.

Thanks,

Terz
Last edited by terzenta; Sep 20th, 2009 at 5:18 pm. Reason: Spelling
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,820
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 213
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Homework:

 
0
  #2
Sep 20th, 2009
In your count function, you have two basic approaches.

1 - Run through string 26 times, each time counting the occurrence of the corresponding letter. Keep track of the letter that has occurred the most.

2 - Run through the string once, incrementing counters stored in an array for each letter. Then run through the array looking for largest count.

Now, is the assignment to write one function, either taking a C-style string or a string object, whichever you prefer to implement? Or must you have a function(s) that will take either type, as the user chooses?

One thing you don't want to do, as you have, is store all the letters as separate strings. Your comparison code is trying to compare a string to a single char from the user's string. That shouldn't work.

If you want all the letters stored, here's a better approach, store them as an array of characters:
  1. char letter[] = {'a', 'b', 'c' ...... 'z' };
Don't own a gun but I'm going to join the NRA.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: terzenta is an unknown quantity at this point 
Solved Threads: 0
terzenta terzenta is offline Offline
Newbie Poster

Re: Homework:

 
0
  #3
Sep 20th, 2009
Thank you very much. Changing
  1. char *letter[] = {"a", "b", "c"... "z"};

to

  1. char letter[] = {'a', 'b', 'c' ...... 'z' };

Seemed to do most of what I needed. I finally got it finished, thanks again for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 327 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC