fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char *trim(char *str)
  5. {
  6. size_t len = 0;
  7. char *frontp = str;
  8. char *endp = NULL;
  9.  
  10. if( str == NULL ) { return NULL; }
  11. if( str[0] == '\0' ) { return str; }
  12.  
  13. len = strlen(str);
  14. endp = str + len;
  15.  
  16. /* Move the front and back pointers to address the first non-whitespace
  17.   * characters from each end.
  18.   */
  19. while( isspace((unsigned char) *frontp) ) { ++frontp; }
  20. if( endp != frontp )
  21. {
  22. while( isspace((unsigned char) *(--endp)) && endp != frontp ) {}
  23. }
  24.  
  25. if( str + len - 1 != endp )
  26. *(endp + 1) = '\0';
  27. else if( frontp != str && endp == frontp )
  28. *str = '\0';
  29.  
  30. /* Shift the string so that it starts at str so that if it's dynamically
  31.   * allocated, we can still free it on the returned pointer. Note the reuse
  32.   * of endp to mean the front of the string buffer now.
  33.   */
  34. endp = str;
  35. if( frontp != str )
  36. {
  37. while( *frontp ) { *endp++ = *frontp++; }
  38. *endp = '\0';
  39. }
  40.  
  41.  
  42. return str;
  43. }
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47. char *sample_strings[] =
  48. {
  49. "nothing to trim",
  50. " trim the front",
  51. "trim the back ",
  52. " trim one char front and back ",
  53. " trim one char front",
  54. "trim one char back ",
  55. " ",
  56. " ",
  57. "\n\n\nNewlines in the front",
  58. "Newlines in the back\n\n\n",
  59. "\nOne newline in front",
  60. "One newline in back\n",
  61. "\n\n\n\n\n\n\n\n\n",
  62. "\n",
  63. "a",
  64. "",
  65. NULL
  66. };
  67. char test_buffer[64];
  68. int index;
  69.  
  70. for( index = 0; sample_strings[index] != NULL; ++index )
  71. {
  72. strcpy( test_buffer, sample_strings[index] );
  73. printf("[%s] -> [%s]\n", sample_strings[index],
  74. trim(test_buffer));
  75. }
  76.  
  77. /* The test prints the following:
  78.   [nothing to trim] -> [nothing to trim]
  79.   [ trim the front] -> [trim the front]
  80.   [trim the back ] -> [trim the back]
  81.   [ trim one char front and back ] -> [trim one char front and back]
  82.   [ trim one char front] -> [trim one char front]
  83.   [trim one char back ] -> [trim one char back]
  84.   [ ] -> []
  85.   [ ] -> []
  86.   [a] -> [a]
  87.   [] -> []
  88.   */
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
[nothing to trim] -> [nothing to trim]
[    trim the front] -> [trim the front]
[trim the back     ] -> [trim the back]
[ trim one char front and back ] -> [trim one char front and back]
[ trim one char front] -> [trim one char front]
[trim one char back ] -> [trim one char back]
[                   ] -> []
[ ] -> []
[


Newlines in the front] -> [Newlines in the front]
[Newlines in the back


] -> [Newlines in the back]
[
One newline in front] -> [One newline in front]
[One newline in back
] -> [One newline in back]
[








] -> []
[
] -> []
[a] -> [a]
[] -> []