String Manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sox
    New Member
    • Oct 2006
    • 1

    #1

    String Manipulation

    Iam going thru a past exam paper and Iam a bit stuck, how can I solve this one:

    In this question you have to write the body of a function:
    A string of even length is sent to function changedString where firstly all occurrences of the
    substring
    es
    in the first half of the string have to be deleted (erased),
    secondly all occurrences of the substring
    es
    in the second half of the string have to be replaced with
    ed
    and finally the changed string has to be returned to the main function.
    Thus, if the string
    The lionesses saw the doves and left the pieces in the houses!
    has for example been sent to the function using SenP in the parameter list, then the string
    The lions saw the dov and left the pieced in the housed!
    has to be returned.
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by sox
    Iam going thru a past exam paper and Iam a bit stuck, how can I solve this one:

    In this question you have to write the body of a function:
    A string of even length is sent to function changedString where firstly all occurrences of the
    substring
    es
    in the first half of the string have to be deleted (erased),
    secondly all occurrences of the substring
    es
    in the second half of the string have to be replaced with
    ed
    and finally the changed string has to be returned to the main function.
    Thus, if the string
    The lionesses saw the doves and left the pieces in the houses!
    has for example been sent to the function using SenP in the parameter list, then the string
    The lions saw the dov and left the pieced in the housed!
    has to be returned.

    You may try:

    string manip_str( const string& s ) {

    string first, second;
    string::size_ty pe current = 0;

    // split the string in two substrings
    first = s.substr( 0, s.size()/2 );
    second = s.substr( s.size()/2, s.size()/2 );

    // erase "es" in the first string
    while( 1 ) {

    current = first.find( "es", current );
    if( current == string::npos )
    break;
    else
    first.erase( current, 2 );
    }

    // replace "es" by "ed" in the second
    while( 1 ) {

    current = second.find( "es", current );
    if( current == string::npos )
    break;
    else
    second.replace( current, 2, "ed" );
    }

    return first + second;
    }

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Code:
      int main(int argc, char** argv[])
      {
      	string str;
      	cout<<"Enter the string with even length"<<endl;
      	getline(cin, str, '\n');
      
      	int len = strlen(str.data());
      	cout<<"Total length:"<<len<<endl;
      	if( (len % 2) != 0 )
      	{
      		cout<<"The string has odd length"<<endl;
      		return 0;
      	}
      	string firstHalf = str.substr( 0, (len/2) );
      	cout<<"FirstHalf:"<<firstHalf<<endl;
      	string secondHalf = str.substr( (len/2), len );
      	cout<<"SecondHalf:"<<secondHalf<<endl;
      
      	//first part of your program
      	for(int i=0; i<firstHalf.size(); ++i)
      	{		
      		int pos = firstHalf.find("es");
      		if(pos == -1)
      			break;
      		firstHalf.erase(pos, 2);		
      	}
      	
      	//second part of your program
      	for(int i=0; i<secondHalf.size(); ++i)
      	{
      		int pos = secondHalf.find("es");
      		if(pos == -1)
      			break;
      		secondHalf.replace(pos, 2, "ed");
      	}
      	
         cout<<"============================================"<<endl;
      	cout<<"Final string"<<endl;
      	cout<<endl;
      	cout<<firstHalf + secondHalf<<endl;
      
      
      	return 0;
      }

      Comment

      Working...