Next character in a string?

This is a discussion on Next character in a string? within the C++ Programming forums, part of the General Programming Boards category; New to C ++ programming have a course in it this semester... I am curious and I don't see it ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Next character in a string?

    New to C ++ programming have a course in it this semester... I am curious and I don't see it in my text, if there is a way to read the next character in a string?

    Suppose the user enters a string of "tree" I need to capture each letter individually and then pass that value off to another part of the program.

    The problem I am working on is to take an user input string and then have it spelled out military style

    So if the user input the word "tree" I need to come back with tango romeo echo echo as my output... and I am trying to do that by breaking the string into single characters and passing their value to a switch, but as my post indicates I'm stuck! LOL

    Any help is greatly appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Loop through the string and use either .at() or [] to access the specific characters.
    Last edited by gamer4life687; 09-17-2009 at 08:13 AM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Is that to say find the length () and then just use a loop to increment until the end of the string is reached? Again I'm a beginner so I am having to look through my text book for something I think could do that... I'm currently on the idea of using a loop to increment a substr function...
    for "tree" length would be 4 so I'm thinking mystring.substr (0,1) loop and then do mystring.substr(1,1) am I on the right track there?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Look at the std::references. string - C++ Reference

    length() is provided for you. There isn't much work for you to do.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Do you need to store the individual letters or just output their translation?
    If just output it's straightforward.
    Code:
    string text = "tree"; //get it from user doesn't matter.
    
    for(int i = 0; i < text.length(); i++){
             switch(text[i]) { 
                    case 'a': 
                    case 't': 
                     ...
             }
    }
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Hmm in your code wouldn't "i" be an number value and not a string or char? so it would never pass a "t" to case 't' for output would it?

    I was thinking
    string ch; //hold the single characters
    string word; //hold the user input string
    int length; //hold the length of the users string

    cin>>word;
    length=word.length(); //find the length
    ch=word.substr (0,1); // create a loop here to increment until lenght of the string is met

    switch (toupper(word[0])) //convert it to upper case so I only have to use capitals in the switch
    {
    case 'A' : cout <<"Alpha ";
    case 'B' : cout <<"Bravo ";
    etc...

    I really don't know if I am on the right track given my little C++ knowledge

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Approach the problem first as just a problem. Not as a C++ assignment and your limited amount of coding won't matter.

    Why do you need substrings for each character? Look at what I posted again above.

    If std::string text = "tree";

    What does text[i] refer to.... What is text[0]...

    text[0] = 't';
    text[1] = 'r';
    text[2] = 'e';
    text[3] = 'e';

    The [] operator returns a char not an integer.

    So if i == 0 the
    switch(text[i]) would be switch(text[0]) which is a character, in this
    case it is the character 't'
    Last edited by gamer4life687; 09-17-2009 at 08:12 AM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Ah ok... I did not know that... I was only familiar with substr being that it's in the chapter on strings in my textbook. So I was trying to figure out how to use it to get the next character in the user input string. I'll give this a whirl and see if I can get it to work! Thanks for the tips!

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Np.

    Substring is useful when you want to store a string of characters from a larger string. You, however, only need a single character which you can access using [] or .at(). You can store the individual characters if you need to but since your only ouputting, I don't see why you would have too.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,674
    Some different ways to loop through a string and do stuff char by char:

    Using iterators:
    Code:
    std::string foo("tree");
    for( std::string::const_iterator cit = foo.begin(); cit != foo.end(); ++cit )
    {
        std::cout << *cit << std::endl;
    }
    Using []:
    Code:
    std::string foo("tree");
    std::string::size_type len = foo.length();
    for( std::string::size_type cur = 0; cur < len; ++cur )
    {
        std::cout << foo[cur] << std::endl;
    }
    Using .at():
    Code:
    std::string foo("tree");
    std::string::size_type len = foo.length();
    for( std::string::size_type cur = 0; cur < len; ++cur )
    {
        std::cout << foo.at(cur) << std::endl;
    }
    All these should output the letters one at a time on separate lines:
    Code:
    t
    r
    e
    e
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21