473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

String Manipulation

1
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.
Oct 17 '06 #1
2 1751
arne
315 Expert 100+
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_type 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;
}
Oct 17 '06 #2
vermarajeev
180 100+
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char** argv[])
  2. {
  3.     string str;
  4.     cout<<"Enter the string with even length"<<endl;
  5.     getline(cin, str, '\n');
  6.  
  7.     int len = strlen(str.data());
  8.     cout<<"Total length:"<<len<<endl;
  9.     if( (len % 2) != 0 )
  10.     {
  11.         cout<<"The string has odd length"<<endl;
  12.         return 0;
  13.     }
  14.     string firstHalf = str.substr( 0, (len/2) );
  15.     cout<<"FirstHalf:"<<firstHalf<<endl;
  16.     string secondHalf = str.substr( (len/2), len );
  17.     cout<<"SecondHalf:"<<secondHalf<<endl;
  18.  
  19.     //first part of your program
  20.     for(int i=0; i<firstHalf.size(); ++i)
  21.     {        
  22.         int pos = firstHalf.find("es");
  23.         if(pos == -1)
  24.             break;
  25.         firstHalf.erase(pos, 2);        
  26.     }
  27.  
  28.     //second part of your program
  29.     for(int i=0; i<secondHalf.size(); ++i)
  30.     {
  31.         int pos = secondHalf.find("es");
  32.         if(pos == -1)
  33.             break;
  34.         secondHalf.replace(pos, 2, "ed");
  35.     }
  36.  
  37.    cout<<"============================================"<<endl;
  38.     cout<<"Final string"<<endl;
  39.     cout<<endl;
  40.     cout<<firstHalf + secondHalf<<endl;
  41.  
  42.  
  43.     return 0;
  44. }
Oct 17 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.