CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    1

    Print the characters excluding the specified substring

    Hi,
    I have a string str ="this is my name"
    start = 's'
    end = 'n'
    my out put should exclude the chars between s and n and print the rest of the string

    str = "thi ame" as my output string.

    Can anyone please help me with c++ code to this problem...

    Thanks

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Print the characters excluding the specified substring

    Well, you could loop over each character in the string; when you find an 's', you skip each character, until you find the 'n'.

    Write up some code, and if you have any questions about the code, just ask.

    Viggy

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    Re: Print the characters excluding the specified substring

    you can do something like that:
    Code:
      #include <string>
    
      std::string lpsz="this is my name";
      std::string start ="s";
      std::string end ="n";
    
      int pos1 = lpsz.find_first_of(start);
      int pos2 = lpsz.find_last_of(end);
    
      std::string result;
      if ( pos1 >= 0 )
      {
        result = lpsz.substr(0,pos1);
        result += " ";
      }
      if ( pos2 >= 0 && pos2 < lpsz.length())
        result += lpsz.substr(pos2+1,lpsz.length());
    
      // result = "thi ame"
    NOTE: this code want not completely debuged!!!

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured