Thread: Remove last character if asterix?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    21

    Remove last character if asterix?

    Hi,

    I have a variable defined as follows:

    char l_string[20]="";

    l_string is populated with values which may or may not have an asterix as the last character i.e.

    abcd
    efghi*
    jklmnop
    aaaaa*aaaaaa*

    If the variable is populated with a string which has an asterix as the last character I would like to remove the asterix.

    What is the best approach for doing this?

    Thanks,

    Manny

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    By assigning the value of 0 to the last character in the array, thus replacing the last character with the string terminator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    s[ strlen(s) ]
    indexes the \0 character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    21
    Can you give more details in your answers please as I do not understand them.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    What's to work out - you go to the end of the string, back one char and see if it's a * or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by manny View Post
    Can you give more details in your answers please as I do not understand them.
    You go to the end of the string, st[strlen(st)] as Salem pointed out, then you back one up, and if it's an asterisk you chance it to an \0. Quite simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Moving a character to stdin?
    By Geolingo in forum C Programming
    Replies: 2
    Last Post: 09-27-2003, 06:37 PM