Danny's Tech Musings



String Operations - V

posted under , , , , by Anonymous



#include <cctype>
#include <string>

#include <cstring>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>

//Remove leading white space

std::string leftTrim( const std::string &s )
{
std::string trimmed;

size_t i;
for( i = 0; i < s.length() && isblank(s[i]); ++i ); //Skip white space


for( ; i < s.length(); ++i )
trimmed.push_back( s[i] );

return trimmed;

}

//Remove trailing white space
std::string rightTrim( const std::string &s )
{
std::string trimmed;
size_t length = s.length(),

i,
j;

for( i = length-1; i >= 0 && isblank(s[i]); --i );


for( j = 0; j <= i; ++j )
trimmed.push_back( s[j] );

return trimmed;

}

//Remove leading and trailing white space
std::string trim( const std::string &s )
{
std::string trimmed;
size_t length = s.length(),

i,
j,
k;

for( i = 0; i < length && isblank(s[i]); ++i ); //Left trim


for( k = length-1; k >= 0 && isblank(s[k]); --k ); //Right trim



for( j = i; j <= k; ++j )
trimmed.push_back( s[j] );

return trimmed;
}


0 comments

Make A Comment
top