Quick question about strings

This is a discussion on Quick question about strings within the C++ Programming forums, part of the General Programming Boards category; I was just wondering if you could use an integer for the character number in a string. For example: Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Quick question about strings

    I was just wondering if you could use an integer for the character number in a string.

    For example:
    Code:
    while(str[num]!=" ")
    {
    code here....
    num++
    }
    or something like that...

    thanks

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    If you are using a char * for your string than yes you can but a character is enclosed in single and not double quotes.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Thanks

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    If you are using a char * for your string than yes
    It also works for accessing the individual characters within the string data type if I'm not mistaken. Just need to make sure you don't try to access a location that isn't there.

    Code:
    string str("Hello there");
    int num = 0;
    while( str[num] != ' ' ) // Potentially dangerous if there is no ' ' character within the string
    {
        cout << str[num];
        ++num;
    }
    Should output just Hello.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    A modified version of hk_mp5kpdw code

    Code:
      
    string str("Hellothere");
    int num = 0;
    while( str[num] != ' ' && str[num] != '\0') 
       cout << str[num++];
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Don't know about strings but it works with char *, all I can say. ^^

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I thought strings weren't null terminated?? But I tested rippers code and it worked fine...

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    I've always used

    Code:
    str.at(num)

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I think PJYelton is right, the STL string is not null-terminated, although you might get lucky on your particular implementation. Checking the length is better:
    Code:
    string str("Hello there");
    int len = str.length();
    int num = 0;
    while( str[num] != ' ' && num < len) 
        cout << str[num++];
    You could of course also use a for loop or iterators. And using at() as [code][/code] suggested checks to make sure you are using a valid index and throws an exception if you are outside the string bounds.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    I'm quite sure STL strings are null terminated. The c_str() function just returns a pointer to the first character and thus the string must be null terminated.
    // Gliptic

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    const char *'s returned from c_str() are required to be null terminated. This makes it likely that the string will keep it's data as a null terminated contigous memory, but it's not required. The null could be inserted during the call to c_str().

  12. #12
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    If STL strings werenīt null terminated how is it possible that c_str() that returns const char * can be used in c-functions (not advisable) e.i strlen? But I must admit that jlou solution is better (safer).
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    According to Josuttis (The C++ Standard Library), if you use the constant version of basic_string::operator[] and you pass in the length of the string (which would be one past the last character), then the value that is generated by the default constructor of the character type is returned. In the case of string (which is really basic_string<char>), the default constructor for char creates the null character, '\0'.

    So, for the constant version of operator[], it is ok to access the null character at the end of the string.

    However, strings can have embedded nulls, so if you didn't know the value of the string beforehand, and you looped through until you got a null character, you might not get the entire string before finding a null.

    Also, if you use the non-constant version of operator[] or if you use at(), then if you do this: str[str.length()], you will get undefined behavior and if you do this: str.at(str.length()), it should throw an exception.

    Given all that, there is nothing that says the implementation has to store the string with a terminating null. Only that it will return a null character if you use the constant version of operator[] on the index one past the last character. I'm not sure how it would get c_str() to work efficiently without doing so, but I also don't care unless I am doing some extremely time critical application and I need to know the implementation details of my standard library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (C/C++)
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 03-19-2005, 08:44 AM
  2. Quick question about replacing laptop harddrive
    By PJYelton in forum Tech Board
    Replies: 4
    Last Post: 01-20-2005, 07:02 PM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  4. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 01:54 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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