Vugen: Will This Code Remove Trailing Spaces?

Let’s have a little fun today – and hopefully I can get some interaction from you C Guru’s out there. How about something cryptic in C? Let’s say I wanted to remove some trailing spaces with as few lines of C code as possible. Will the example below work?

static char* rtrim( char* s)
 int i;
 if (s){i = strlen(s); while ((--i)>0 && isspace(s[i]) ) s[i]=0;}
 return s;

Why or why not? Care to explain what this code is doing? Comments are open and welcome.

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Scott Moore

About Scott Moore (153 articles)

With over 20 years of IT experience with various platforms and technologies, Scott has tested some of the largest applications and infrastructures in the world. He is a Certified Instructor and Certified Product Consultant in HP’s LoadRunner and Performance Center products. He currently holds HP certifications for ASE, ASC, and CI. A thought leader in the APM space, he speaks regularly at IT conferences and events

  • Mark Tomlinson

    Sure it will only trim trailing spaces…but not intermediary spaces or leading spaces:

    Virtual User Script started at : 7/9/2012 6:07:00 PM
    Starting action vuser_init.Ending action vuser_init.
    Running Vuser…
    Starting iteration 1.
    Starting action Action.
    Action.c(11): |this has spaces|
    Action.c(12): |thisdoesnot|
    Action.c(13): | leading space|
    Action.c(14): |trailingpsace|
    Ending action
    Action.Ending iteration 1.
    Ending Vuser…
    Starting action vuser_end.
    Ending action vuser_end.
    Vuser Terminated.

    • Scott Moore

      So now we just need to write something just as cryptic for the leading characters! :)