5 Years
Discussion Span
Last Post by Lucaci Andrew

Use substr to extract the characters before the word, after that use it again to extract the raw of characters after the word, then join both strings by summing them.


string sentence="blah blah blah";
string before=sentence.substr(0,5);
string after=sentence(10,14);
string withoutTheWord=before+after;

Are you using a std::string? If so, you can use a combination of the find and erase methods:

std::string s( "The quick brown fox jumps over the lazy dog" );

std::cout << s << std::endl;

std::string::size_type position = s.find( "jumps" );
s.erase( position, 6 );  // Use a size of 6 to include the extra space after the word

std::cout << s << std::endl;

If you're not allowed to use std::string then you will have to use the functions in <cstring>. You can use std::strstr to find a substring in a C-style char array. Then you can use std::strcpy to build a new string with the missing parts.

Finally, if you're not allowed to use either of these two methods, then you'll have to write your own function to do the comparison.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.