C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
|
#include <stdio.h> #include <string.h> #include <ctype.h> void Trim (char *src); char* Trim2 (char *src); int main (int argc, char *grgv[]) { Char str[] = "good, Morning"; printf ("str=%s; strlen=%d\n ", str, strlen (str)); TRIM2 (str); printf ("str=%s; strlen=%d\n ", str, strlen (str)); return 0; } /***************************************************************************** * * Function Name: Trim2 (Dependent library function) * * Function Description: Given a c-string variable (not literal constant), remove the blank at both ends of the character * * Current version: * * Author: johnnyhu * * Modified: * * Input parameters: Char *src---> with ' c-string ' * * OUTPUT parameters: Char *src---> Formatted (remove blank characters at both ends) * * return Result: SRC * * Usage Condition: c-string variable of non-letter value constant ****************************************************************************/ char* Trim2 (char *src) { char *ori_src = src; char *begin = src; Char *end = src + strlen (src); if (begin = = end) return ori_src; while (Isblank (*begin)) ++begin;
while (Isblank (*end) | |! ( *end)) --end; if (Begin > End) { *src = ' n '; return ORI_SRC; }
printf ("BEGIN=%1.1S; End=%1.1s\n ", begin, end); while (begin!= end) { *src++ = *begin++; } *src++ = *end; *src = ' n '; return ORI_SRC; } /***************************************************************************** * * Function Name: Trim * * Function Description: Given a c-string variable (not literal constant), remove the blank at both ends of the character * * Current version: * * Author: johnnyhu * * Modified: * * Input parameters: Char *src---> with ' c-string ' * * OUTPUT parameters: Char *src---> Formatted (remove blank characters at both ends) * * Return Result: None * * Usage Condition: c-string variable of non-letter value constant ****************************************************************************/ void Trim (char *src) { char *begin = src; char *end = src; while (*end++) { ; Statement is null } if (begin = = end) return; while (*begin = = ' | | *begin = = ' t ') ++begin; while ((*end) = = ' | | *end = = ' | | *end = = ' t ') --end; if (Begin > End) { *src = ' n '; Return } printf ("BEGIN=%1.1S; End=%1.1s\n ", begin, end); while (begin!= end) { *src++ = *begin++; } *src++ = *end; *src = ' n '; Return } |
Results:
If there is a bug, please ask. Have a good time!!!