SteelProgramming | +38 098 888 58 89 Alexanders.berezovich Skypesteelprogram@gmail.com E-mail |
Realization trim on C++ for char* with exampleParameters:
#define TRIM_LEFT 0 // #define TRIM_RIGHT 1 #define TRIM_BOTH 2 int trim(char* Str, int Type) { int StrLn = strln(Str) + 1; if ((StrLn == 0) || (Type < TRIM_LEFT) || (Type > TRIM_BOTH)) { return 0; } char *NewStr = new char[StrLn]; int IdxSrc = -1, IdxDest = 0; if ((Type == TRIM_LEFT) || (Type == TRIM_BOTH)) { bool InText = false; while(Str[++IdxSrc]) { if (!InText && (Str[IdxSrc] != ' ') && (Str[IdxSrc] != '\n') && (Str[IdxSrc] != '\t')) { InText = true; } if (InText) { NewStr[IdxDest++] = Str[IdxSrc]; } } NewStr[IdxDest] = '\0'; } else { IdxDest = StrLn - 1; strcpy_s(NewStr, StrLn, Str); } if ((Type == TRIM_RIGHT) || (Type == TRIM_BOTH)) { while(--IdxDest > 0) { if ((NewStr[IdxDest] != ' ') && (NewStr[IdxDest] != '\n') && (NewStr[IdxDest] != '\t')) { break; } } NewStr[IdxDest] = '\0'; } strcpy_s(Str, StrLn, NewStr); delete NewStr; return IdxDest; } int main() { char *Str = new char[9]; strcpy_s(Str, 9, " hello "); trim(Str, TRIM_BOTH); std::cout << "#" << Str << "#" << std::endln; _gecth(); return 0; } You can add comment on current page, or on forum page there. Last comment | |
powered by Steel Programming |