| | |
Homework:
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
(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.
Like I said, I just have no idea where to go. Any help would be appreciated.
Thanks,
Terz
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.
C++ Syntax (Toggle Plain Text)
//This program takes in a pointer to a c-string and returns the character that appears most frequently #include <iostream> #include<string> #include<cstring> using namespace std; int main() { int letterIndex, stringCounter, highestNumber, highestLetter; int counter[80]; 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"}; const int length = 80; // Maximum length for string char line[length]; // Array of char int numberOfLetters = 0, index = 0; letterIndex = 0; stringCounter = 0; highestNumber = 0; // Read a string into the character array. cout << "Enter in a word or sentence less than " << length-1 << " characters:\n"; cin.getline(line, length); numberOfLetters = strlen(line); while (letterIndex < 26) {counter[letterIndex] = 0; letterIndex++;} while (letterIndex <= 25) { while (stringCounter <= numberOfLetters){ cout << letter[letterIndex]; if (letter[letterIndex] == line[stringCounter]) {cout << "it worked";} //counter[letterIndex]++;} stringCounter++;} letterIndex++;} letterIndex = 0; while (letterIndex < 26) {cout << counter[letterIndex] << endl; letterIndex++;} letterIndex = 0; //while (letterIndex <= 25) { // if (counter[letterIndex] > highestNumber) { // highestNumber = counter[letterIndex]; // highestLetter = letterIndex;} // letterIndex++; // } cout << "The letter you used most frequently was: " ;//<< letter[highestLetter] << ".\n"; return 0; }
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
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 - 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:
C++ Syntax (Toggle Plain Text)
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.
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.
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
Thank you very much. Changing
to
Seemed to do most of what I needed. I finally got it finished, thanks again for the help.
C++ Syntax (Toggle Plain Text)
char *letter[] = {"a", "b", "c"... "z"};
to
C++ Syntax (Toggle Plain Text)
char letter[] = {'a', 'b', 'c' ...... 'z' };
Seemed to do most of what I needed. I finally got it finished, thanks again for the help.
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- I am having homework isues. Please Help (Java)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Class unresolved external error
- Next Thread: Input a number and store it in an array
Views: 327 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes client code compile compiler constructor conversion convert count delete dll dynamic encryption error file files form forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework http iamthwee input int integer lazy linker list loop loops map math matrix member memory multidimensional network newbie news number object objects opengl output parameter pointer pointers problem program programming project qt random read recursion recursive reference search sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio win32 window windows winsock






