You are here:

C++/Regarding String Manipulation

Advertisement


Question
How to copy characters after the last occurence of delimeter from a string.

Eg:think am having the original string like "this/will/be/ur/great/day/goodmorning"

in output i want to copy the string "goodmorning" ie, after last ocuurence of the delimiter "/".

can u please suugest valuable solution for this.
thanks alot in advance.

Answer
How about some logic like so (in pseudo code):

   delimiter : constant character    = '/'
   begin     : string position type  = 0
   end       : string position type  = nopos   // nopos represents no position
                                               // e.g. std::string::npos

   repeat:
       set end to position of next delimiter in original string starting from begin position

       if delimiter found
           set section string as copies of characters from begin position up to
           but not including end position from original string

           begin = end + 1 // skip over delimiter
       end if

   while delimiter found

   // ### collect last part of original string ###

   if begin position is not past the last character of the original string
       set section string as copies of characters from begin position to
       the end of the original string
   endif

Section strings are the sections of the original string between delimiters, or the last part after the last delimiter to the end of the string. I have not shown how you store these. I assume that if the begin position is one past the last character in the original string that the find operation returns not found rather than fails by e.g. throwing an exception!

Here is an example implementation using std::string which stores the sections of the original string as strings in a vector (single dimension array) [Note: I am assuming you have access to a fairly ISO standard C++ (and C++ library) implementation]:

   #include <string>     // for std::string
   #include <vector>     // for std::vector to store sections
   #include <algorithm>  // for std::copy
   #include <iostream>   // for std::cout
   #include <iterator>   // for std::ostream_iterator

   int main()
   {
       typedef std::vector<std::string>    StringSectionCollectionType;
       
       std::string originalString("this/will/be/ur/great/day/goodmorning");
       StringSectionCollectionType sections;
       
       char const                delimiter('/');
       std::string::size_type    begin(0);
       std::string::size_type    end(std::string::npos);
       
       do
       {
           end = originalString.find( delimiter, begin );
           
           if ( std::string::npos != end )
           {
               std::string section( originalString.substr(begin, end-begin) );
               sections.push_back( section );
               begin = end + 1;
           }
       }
       while ( std::string::npos != end );
       
   // ### collect last part of original string ###
       if ( begin < originalString.length() )
       {
       
       // Note: use from position to end of string form of substr operation
           std::string section( originalString.substr(begin) );
           sections.push_back( section );
       }

       std::copy
       ( sections.begin()
       , sections.end()
       , std::ostream_iterator<std::string>(std::cout, "\n")
       );        
       return 0; // only required for certain broken compilers
   }

The implementation above works with most variations of originalString - empty string, single "/" string, string with '/' as last character. You might wish to consider what should happen when a string contains more than one consecutive '/' e.g. "this/will//be/ur//great/day/goodmorning". Currently there will be an empty section string between each consecutive '/' so the example would yield sections: "this", "will", "", "be", "ur", "great", "day", "goodmorning".

Note that although I use the names begin and end these are not iterator types as returned by std::string::begin() and std::string::end(). Although using iterators would be nice the interface to std::string and its various operations makes it easier in this case to use character position values (and deduce a length for substr from them to boot!).

Although not immediately obvious we can replace the do .. while loop with a for loop in which the find is performed as the initial and part the loop update statements and the while condition is used as the for loop condition. We also have to update begin in the update part of the for loop. This the gives us a loop like so:

   for
   ( end = originalString.find( delimiter, begin )
   ; std::string::npos != end
   ; begin = end + 1, end = originalString.find( delimiter, begin )
   )
   {
       std::string section( originalString.substr(begin, end-begin) );
       sections.push_back( section );
   }

I use the C++ standard library copy algorithm together with an output stream iterator. The std::copy algorithm copies values from one sequence to another, while an output stream iterator adapts a std::ostream object to look like a iterator defined sequence. I use std::copy to copy the sequence of all items in the sections vector, from first to last, to std::cout with a newline between each item. The output should look like this:

   this
   will
   be
   ur
   great
   day
   goodmorning

Another variation, using the original loop structure, would be to place the collection of the last section in the loop:

       do
       {
           end = originalString.find( delimiter, begin );
           
           if ( std::string::npos != end )
           {
               std::string section( originalString.substr(begin, end-begin) );
               sections.push_back( section );
               begin = end + 1;
           }
           else if ( begin < originalString.length() )
           {
           // ### collect last part of original string ###
               std::string section( originalString.substr(begin) );
               sections.push_back( section );
           }
       }
       while ( std::string::npos != end );

But this does not really gain us very much, if anything at all, in the way of additional clarity or brevity.

You may be able to spend time to produce other variations on this theme but we will always have a special case to check for and handle when no further delimiters are found. It is a fact of life that these special cases tend to complicate and make less elegant otherwise simple procedures.Ho hum...

In any case I hope you find this of use.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.