**resize String**

This is a discussion on **resize String** within the C++ Programming forums, part of the General Programming Boards category; hi everyone, my question is if string is odd, add '0' at the end of the string, if it is ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Talking **resize String**

    hi everyone,

    my question is if string is odd, add '0' at the end of the string, if it is even, tats alright. hope u guyz understand wat i mean.

    example string size is odd [4], tats mean 0, 1, 2, 3, and i want to make it to even so add one size to it and it become odd[5] = 0, 1, 2,3,4,5....

    i want to ask that how can i resize the string ?


    this is my code

    Code:
        while(str.size()%2!=0)
        {
            str.resize(str.size() + 1, 0);
        }    
        cout<<str<<endl;

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    the string automaticaly changes size as you add/remove characters from it.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    i am sorry, i dont understand

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Code:
    string str, str2;  //str size = 0; str2 size = 0;
    str = "This is a ";  //str size = 11
    str2 = "string";  //str size = 7
    
    str += str2; //str size = 17
    
    //size has changed from 11 to 17 automaticaly.
    My Website
    010000110010101100101011
    Add Color To Your Code!

Popular pages Recent additions subscribe to a feed

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