Ejemplo n.º 1
0
	/*
	 * Returns a string that Spoonerism has been performed on.
	 * A string is spoonerized by swapping the beginning of the first word in
	 * the string with the beginning of the second word.
	 * <param_name = "first_len"> : Represents the number of characters from
	 * the first word to swap.
	 * <param_name = "second_len"> : Represents the number of characters from
	 * the second word to swap.
	 * If the word frequency of the string, along with first_len and second_len
	 * exceed 2, then the original string is returned.
	 */
	STR spoonerize( STR str, const int &first_len, const int &second_len ) {
		if ( word_frequency( str ) > 2 ||
			( ( first_len < 1 || first_len > 2 ) || 
			( second_len < 1 || second_len > 2 ) ) ) 
			return str;
		
		std::stringstream ss;
		std::size_t space_pos = str.find( ' ' );

		if ( first_len == 1 && second_len == 1 ) {
			std::swap( str[0], str[( space_pos + 1 )] );
			return str;
		} else if ( first_len == 2 && second_len == 1 ) {
			ss << str[( space_pos + 1 )] << str.substr( 2, ( space_pos - 1 ) ) <<
				str.substr( 0, 2 ) << str.substr( ( space_pos + 2 ), ( str.size() - 1 ) );
		} else if ( first_len == 1 && second_len == 2 ) {
			ss << str.substr( ( space_pos + 1 ), 2 ) << str.substr( 1, space_pos ) <<
				str[0] << str.substr( ( space_pos + 3 ), ( str.size() - 1 ) );
		} else if ( first_len == 2 && second_len == 2 ) { 
			ss << str.substr( ( space_pos + 1 ), 2 ) << str.substr( 2, space_pos - 1 ) <<
				str.substr( 0, 2 ) << str.substr( ( space_pos + 3 ), ( str.size() - 1 ) );
		}

		str = ss.str();
		return str;
	}
Ejemplo n.º 2
0
	/*
	 * Returns a vector of strings from the specified string, which has
	 * been sliced at each position where the delimiter appears.
	 */
	std::vector<STR> distribute( STR str, CSTR_R delimiter ) {
		std::vector<STR> slices;
		std::size_t pos = 0;

		while ( ( pos = str.find( delimiter ) ) != STR::npos ) {
			slices.push_back( str.substr( 0, pos ) );
			str.erase( 0, ( pos + delimiter.length() ) );
		}

		return slices;
	}
Ejemplo n.º 3
0
static bool ContainsOnlyCharsT(const STR& input, const STR& characters)
{
    for(typename STR::const_iterator iter=input.begin();
        iter!=input.end(); ++iter)
    {
        if(characters.find(*iter) == STR::npos)
        {
            return false;
        }
    }
    return true;
}
Ejemplo n.º 4
0
 static void SplitStringUsingSubstrT(const STR& str,
     const STR& s, std::vector<STR>* r)
 {
     typename STR::size_type begin_index = 0;
     while(true)
     {
         const typename STR::size_type end_index = str.find(s, begin_index);
         if(end_index == STR::npos)
         {
             const STR term = str.substr(begin_index);
             STR tmp;
             TrimWhitespace(term, TRIM_ALL, &tmp);
             r->push_back(tmp);
             return;
         }
         const STR term = str.substr(begin_index, end_index-begin_index);
         STR tmp;
         TrimWhitespace(term, TRIM_ALL, &tmp);
         r->push_back(tmp);
         begin_index = end_index + s.size();
     }
 }
Ejemplo n.º 5
0
	/*
	 * Returns the portion of the string after the delimiter.
	 */
	STR slice_after( STR str, CSTR_R delimiter ) {
		std::size_t pos = str.find( delimiter );
		return str.substr( ( pos + delimiter.size() ) );
	}
Ejemplo n.º 6
0
	/*
	 * Returns the portion of the string before the specified delimiter.
	 */
	STR slice_before( STR str, CSTR_R delimiter ) {
		std::size_t pos = str.find( delimiter );
		return str.substr( 0, pos );
	}