dyanamic strings in c++ help, how do i erase the last letter on a buffer?

This is a discussion on dyanamic strings in c++ help, how do i erase the last letter on a buffer? within the C++ Programming forums, part of the General Programming Boards category; ie, char *buffer; buffer contains "hello"; i would like to erase the letter 'o', how would i do that? thanks,...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    dyanamic strings in c++ help, how do i erase the last letter on a buffer?

    ie, char *buffer;

    buffer contains "hello";

    i would like to erase the letter 'o',
    how would i do that?

    thanks,

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    can't you do

    Code:
    strcat(buffer,"\b");
    ? I have never tried this so...
    Last edited by ErionD; 04-06-2002 at 09:52 AM.

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Why not just replace the last character with a " "?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    wow, it worked hehe, inserting a ' '; thanks guys!

    well i guess strcat would work too,

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Code:
    int count;
    //finds end of string
    for(count  = 0 ; buffer[counter] ; count++)
    ;//required
    count--;//set count to last character
    buffer[count] = '\0'; //overwrite last character
    this requires a null terminated string and will leave an extra null character at the end of the string. Since it is dynamic, you could create a new array first, do the change & copy, then delete the first array
    Last edited by Syneris; 04-08-2002 at 02:08 PM.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    this is a bit faster:
    buffer[strlen(buffer) - 1] = ' ';
    not sure if you need the "- 1" part because i dont know if strlen counts the at the end of the string as part of the string. also, i dunno if strlen works around zero or one, so try both.
    hope i didnt just confuse you .

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    lol! i have no idea why i said to use a loop instead of strlen...
    you should subtract 1 from strlen

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    I don't think you need the -1 when using strlen because it doesn't count the null character . strlen("hello") should return 5 instead of 6.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    you do because arrays start with 0 instead of 1

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    just dont wright the "o"

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    buffer[strlen(buffer) - 1] = ' ';

    what happens when strlen(buffer)==0?

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    spell check wriiting i keep forgeting

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Originally posted by rip1968
    buffer[strlen(buffer) - 1] = ' ';

    what happens when strlen(buffer)==0?
    compiler won't give an error, but you will still be writing to memory. This almost always cause an error when the program is run.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    len=strlen(buffer);
    if(len!=0)
    buffer[len-1]='\0';


    you're right, you do have to subtract 1 to get to the right index, I was just thinking about returning the size of buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  2. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 10:12 PM
  3. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 12:08 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM

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