Thread: string operations

  1. #1
    Registered User
    Join Date
    Nov 2005

    string operations

    Bades,I want to parse through the string .The below code is compliing but i am not getting the desired output.

    string str;


    int size=str.size();

    for (int i=0; i<=size ;i++ )


    can u please tell what is worng here..

  2. #2
    Registered User
    Join Date
    Aug 2005
    what output are you expecting? Since you did not put a space between characters they will all be scrunched up together so that it looks like out output the string instead of individual characters. try this
    cout<<str[i] << " ";

  3. #3
    Registered User
    Join Date
    Dec 2001
    Specify what the undesired result is.

    If it cuts off at spaces, use cin.getline();

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Milwaukee, WI, USA
    Can't say for sure since you don't note what kind of error you're getting, but I'd guess you're entering a space in the input intended for str. cin only grabs from the input stream until the next whitespace, so if you enter:

    why is this not working

    to the program, str will only be assigned "why".

    Also, make sure you're using code tags properly. That's what the edit button is for.

    enter your code here

    Will end up looking like this:
    enter your code here
    (Thanks quzah)
    Last edited by Decrypt; 11-02-2005 at 08:27 PM.
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    Jan 2005
    The only thing that is "wrong" with your code is that you access a character passed the string's bounds. Using [] with an index greater than or equal to size() is undefined.

    You should use i < size instead of i <= size.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Just to explain a little further what Daved is saying. If you have an string of length 3,

    string str = "yes";

    you can treat it as an array. But, arrays start at index position 0. So, the 'y' is at index position 0, the 'e' is at index position 1, and the 's' is at index position 2. Note that index position 3 doesn't exist. The general rule is that index values go up to one less than the size of the array.

    You also might want to consider proofreading. Is it possible you didn't notice that your code tags failed to work?
    Last edited by 7stud; 11-02-2005 at 04:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standard String operations
    By kolucoms6 in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2008, 12:47 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  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