User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,842 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Apr 29th, 2007
Views: 2,642
Here is a simple implementation of implementing a simple Left trim (ltrim) and Right trim(rtim) of unwanted characters from a C style string. (null terminated strings). Doesn't suffer the overheads of the memmove implementation in which the worst case scenario (a string containing all junk characters) is approx. N^2 / 2.

Comments, constructive criticisims are welcome.
Last edited : Apr 29th, 2007.
c Syntax | 5 stars
  1. #include <stdio.h>
  2.  
  3. char* rtrim(char* string, char junk);
  4. char* ltrim(char* string, char junk);
  5.  
  6. int main()
  7. {
  8. char testStr1[] = " We like helping out people ";
  9. char testStr2[] = " We like helping out people ";
  10. char testStr3[] = " We like helping out people ";
  11. printf("|%s|", testStr1);
  12. printf("\n|%s|", ltrim(testStr1, ' '));
  13. printf("\n\n|%s|", testStr2);
  14. printf("\n\n|%s|", rtrim(testStr2, ' '));
  15. printf("\n\n|%s|", testStr3);
  16. printf("\n|%s|", ltrim(rtrim(testStr3, ' '), ' '));
  17. getchar();
  18. return 0;
  19. }
  20.  
  21.  
  22. char* rtrim(char* string, char junk)
  23. {
  24. char* original = string + strlen(string);
  25. while(*--original == junk);
  26. *(original + 1) = '\0';
  27. return string;
  28. }
  29.  
  30. char* ltrim(char *string, char junk)
  31. {
  32. char* original = string;
  33. char *p = original;
  34. int trimmed = 0;
  35. do
  36. {
  37. if (*original != junk || trimmed)
  38. {
  39. trimmed = 1;
  40. *p++ = *original;
  41. }
  42. }
  43. while (*original++ != '\0');
  44. return string;
  45. }
  46. /*
  47.  
  48. | We like helping out people |
  49. |We like helping out people |
  50.  
  51. | We like helping out people |
  52.  
  53. | We like helping out people|
  54.  
  55. | We like helping out people |
  56. |We like helping out people|
  57.  
  58.  */
Comments (Newest First)
~s.o.s~ | Rebellion Revamped | May 26th, 2007
> How to you handle tabs?
If you will notice, this is not a generic function, the way we have in Python, Ruby and other languages. You need to pass the character to be stripped. If you want to strip off tabs, pass the junk character to be '\t'.
Ene Uran | Veteran Poster | May 24th, 2007
How to you handle tabs?
StrikerX | Newbie Poster | May 23rd, 2007
Nice one, thanks .
Nutty | Newbie Poster | May 3rd, 2007
it is a good, i like this type of programe
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC