Task: Strip control codes and extended characters from a string

C: #include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXBUF 256  
#define STR_SZ 100   



int ascii (const unsigned char c);

int ascii_ext (const unsigned char c);

unsigned char* strip(unsigned char* str, const size_t n, int ext );


 
int ascii (const unsigned char c) 
{  
  unsigned char min = 32;   
  unsigned char max = 126;  

  if ( c>=min && c<=max ) return 1;

  return 0;
} 


 
int ascii_ext (const unsigned char c) 
{  
  unsigned char min_ext = 128;   
  unsigned char max_ext = 255;

  if ( c>=min_ext && c<=max_ext )
       return 1;

  return 0;
} 




unsigned char* strip( unsigned char* str, const size_t n, int ext) 
{ 

  unsigned char buffer[MAXBUF] = {'\0'};

  size_t i = 0;  
  size_t j = 0;  

  size_t max = (n<MAXBUF)? n : MAXBUF -1;  

  while (i < max )
    {
      if ( (ext && ascii_ext(str[i]) ) ||  (ascii(str[i]) ) )    
	{
	  buffer[j++] = str[i]; 
	}      
      i++;
    }

  memset(str, '\0', max); 

  i = 0;               

  while( i < j)
    {
      str[i] = buffer[i]; 
      i++;
    }

  str[j] = '\0';  

  return str;  
}


int main( int argc, char** argv)
{
  enum {ASCII=0, EXT=1}; 

  unsigned int seed = 134529;  

  
  unsigned char badstring[STR_SZ] = {'\0'};
  unsigned char bs_2[STR_SZ]      = {'\0'};
 
  unsigned char* goodstring = NULL;
  unsigned char* goodstring_ext = NULL;

  size_t i = 0;

  srand(seed); 

  fprintf(stdout, "Original:\t" );

  
  for (i = 0; i < STR_SZ; i++)
    {
      badstring[i] = (unsigned char) ( rand () & (unsigned char)0xFF );
      fprintf(stdout, "%c", badstring[i] );
    }
  fprintf(stdout, "\n");

   
   memcpy(bs_2, badstring, STR_SZ * sizeof(unsigned char) ); 

   goodstring_ext = strip( badstring, STR_SZ, EXT); 

   fprintf(stdout, "\nEXT:\t%s\n" , goodstring_ext );

   goodstring = strip( bs_2, STR_SZ, ASCII); 
  
   fprintf(stdout, "\nASCII:\t%s\n" , goodstring );
  
return 0;
}


C++: #include <string>
#include <iostream>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/casts.hpp>
#include <ctime>
#include <cstdlib>
using namespace boost::lambda ;

struct MyRandomizer {
   char operator( )( ) {
      return static_cast<char>( rand( ) % 256 ) ;
   }
} ;

std::string deleteControls ( std::string startstring ) {
   std::string noControls( "                                        " ) ;
   
   std::remove_copy_if( startstring.begin( ) , startstring.end( ) , noControls.begin( ) ,
	 ll_static_cast<int>( _1 ) < 32 && ll_static_cast<int>( _1 ) == 127 ) ;
   return noControls ;
}

std::string deleteExtended( std::string startstring ) {
   std::string noExtended ( "                                        " ) ;
   std::remove_copy_if( startstring.begin( ) , startstring.end( ) , noExtended.begin( ) ,
	 ll_static_cast<int>( _1 ) > 127 || ll_static_cast<int>( _1 ) < 32 ) ;
   return noExtended ;
}
   
int main( ) {
   std::string my_extended_string ;
   for ( int i = 0 ; i < 40 ; i++ ) 
      my_extended_string.append( " " ) ;
   srand( time( 0 ) ) ;
   std::generate_n( my_extended_string.begin( ) , 40 , MyRandomizer( ) ) ;
   std::string no_controls( deleteControls( my_extended_string ) ) ;
   std::string no_extended ( deleteExtended( my_extended_string ) ) ;
   std::cout << "string with all characters: " << my_extended_string << std::endl ;
   std::cout << "string without control characters: " << no_controls << std::endl ;
   std::cout << "string without extended characters: " << no_extended << std::endl ;
   return 0 ;
}