Thread: Please help with a c++ string

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Please help with a c++ string

    Hi guys, first off this is not a homework assignment. During my break from school I am trying to learn some more c++. I am trying to write a program that uses a string and that is supposed to have the user enter a string, then enter a char, then the program is supposed to tell you how many times the char you entered is in the string you typed.(example - the string i enter is "hey whats up" and the char i enter is "h", the program is supposed to tell me that "h" occured 2 times). The book I am using tells me that I should use a loop that starts the index value at 0 then changes the index to one value passed the index where char was found (which is confusing to me).

    I am having a hard time with this one..help would greatly be appreciated. Below is some code I have so far. please help me out. thank you

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    
    {
    string message;
    char letter;
    int numletter;
    int i;
    
    
    
    cout << "Enter a message\n";
    
    
    getline(cin, message);
    
    cout << "Enter a letter\n";
    cin >> letter;
    
    numletter = message.find(letter);
    
    for (i = 0; i <= numletter; i++)
    
    
    
    
    system ("pause");
    
    return 0;
    
    }
    I don't really know what to put below the for statement. Thanks guys

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Not really sure of the point but:
    Code:
    const char * testString = "Now is the time for this test to end";
    
    
    int main(int argc, char *argv[])
    {
       int nRC = 0;
       cout << "lettersearch_test 1.0" << endl;
       int count = 0;
       char testChar = 'e';
       for(int x = 0; x < strlen(testString); x++)
       {
          if(testString[x] == testChar)
          {
    	 count++;
          }
    
    
       }
       cout << "\'" << testChar << "\' was found " << count << " times." << endl;
       return nRC;
    }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    In a loop, you want to find the index of where the first occurrence of where the letter is, which is what "find" does with no given index. If it was found (if the result is not npos), then you increase a counter. You continue this process: doing a find from where the last occurrence was found + 1, check if it was found, increase counter. Heres the reference for "find" with examples at the bottom. In particular, the examples show you how to search the string starting from some index, and also checking whether the character/string was found or not (its not found if it returns npos). find - C++ Reference

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Sorry, missed the desire for a C++ angle...
    Code:
       string Str = testString;
       string searchStr = "e";
       int nPos = 0;
       int nCurPos = nPos;
       int cpp_count = 0;
       while(nPos != std::string::npos)
       {
          nPos = Str.find(searchStr, nCurPos);
          if(nPos != std::string::npos)
          {
    	 cpp_count++;
    	 nCurPos = nPos +1;
          }
       }
       cout << "CPP method reveals there are " << cpp_count << " instances of " << searchStr << " in " << Str << endl;
    That should do it..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If your using stl functions, you may as well use count:

    Code:
    mycount = count (message.begin(), message.end(), letter);
    But jeffcobb's first example is what the book was describing.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by King Mir View Post
    If your using stl functions, you may as well use count:

    Code:
    mycount = count (message.begin(), message.end(), letter);
    But jeffcobb's first example is what the book was describing.
    You know Mr. King Mir, you bring to light something that at first brings shame. I have been doing this for a lot of years, the point being since before and during the birth of the STL. As anyone who has made the transition from C to C++ will tell you the first efforts tend to be C code dressed up as C++....and my partial use of the STL is an embarrassing example of this. Its like I know about 20% of the STL well and use it all the time but as such am missing out on a lot of the power available for free. I have been using the STL since all that was available were the Stepenov headers (sp) and feel bad that I have not grown much beyond what I have needed for any given task.....This is one way I hope to learn much from this forum and those who inhabit it.

    Thank you.

    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I've been using the STL for some time now and also felt the same way you did jeffcobb about it. Because of this I recently purchased Effective STL by Scott Meyers. I highly recommend the book b/c it answers a lot of common questions and corrects common misunderstandings about the STL. You can get it at Amazon for about $30.

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    TKs Bubba; I will find it as an ebook for my Cool*ER book reader (my personal salvation).
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    jeffcobb, I noticed in your version of the code you don't have any cin statements? The program I am writing asks the user to write a line of text. Then after they write the text they enter whatever letter they choose to be searched and told how many times it occurs. It looks as if your code is always searching for the letter 'e'.

    Also is your code a separate function? Or is it all in main ()?

    Thanks for your help guys, I am trying my hardest to learn this.

    One last thing. What is STL?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by nick753
    What is STL?
    The Standard Template Library, which is a precursor to some parts of the C++ standard library (mostly the parts involving containers, generic algorithms and iterators), and is often still used to refer to those parts.

    EDIT:
    Quote Originally Posted by nick753
    I noticed in your version of the code you don't have any cin statements? The program I am writing asks the user to write a line of text. Then after they write the text they enter whatever letter they choose to be searched and told how many times it occurs. It looks as if your code is always searching for the letter 'e'.
    The idea is to understand what an example does and adapt it for your needs.

    Quote Originally Posted by nick753
    Also is your code a separate function? Or is it all in main ()?
    If you use King Mir's suggestion, then for such a simple program just a global main function will do.
    Last edited by laserlight; 12-16-2009 at 11:32 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    What she said...

    Seriously I just grabbed a spare project I had on the hard drive, dumped the code and added this. I didn't think the string input was the problem, rather counting the characters. Thus I figured it was OK just to hard-code the input since it was just an example...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Well can you tell or show me how to make the program search for whatever char the user types in?

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    @nick753
    You already have code that allows the user to enter a string and specify a letter. I used mostly the same variable names as you so that it would be clear how to plug the code in to what you have.
    you will also need to #include <algorithm>.

    @jeffcobb
    I reccomend Josuttis's The C++ Standard Library for learning STL.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    King Mir, When I use your code, the following happens.

    Say I type in the word "Hello"
    Then I type the char "l"
    It will display that there are 2 "l" ' s but it will display those 2 l's three times. Basically no matter what string I type, it will display the char that I choose one more time than how many of that char there is.

  15. #15
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Quote Originally Posted by nick753 View Post
    .... Basically no matter what string I type, it will display the char that I choose one more time than how many of that char there is.
    That should be a nice clue as to your bug. Something repeating like a loop, and using the number of times the char was found ++.
    Asking a question you already know the answer to might teach you something you did not know...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM