0

I've been dealing with C++ strings and currently I am wondering how can I split the article of a string and the data that's left.

Let's say I have this string:

a gold coin

How can I split the article which in this case is a into a separate string and then get all the other data contents into a different string which in this case is gold coin?

Please note that the article can sometimes be an and sometimes have no article.

Edit I am not trying to split strings from a space token I am splitting special words from the string in condition for extracting a pronoun article name and the pronoun string.

Raúl Sanpedro
  • 266
  • 4
  • 15
  • Will you always have a simple `[article] [nouns]` layout or do you need to parse complex sentences? – Neil Kirk Oct 10 '14 at 01:31
  • 1
    I would love to write an answer but I updated my system and it borked everything. Look into std::string::substr(). You could check for mystring.substr( 0, 3 ) == "an " || mystring.substr( 0, 2 ) == "a ". If you match either, mystring.substr( 4 ) in the first case of mystring.substr( 3 ) in the 2nd case should give you the rest of the string. – kiss-o-matic Oct 10 '14 at 01:31
  • http://www.cplusplus.com/reference/string/string/substr/ – kiss-o-matic Oct 10 '14 at 01:32
  • the linked question goes over strategies on how to use `substr`, `replace`, etc to split a string. – Rapptz Oct 10 '14 at 01:34
  • No problem! Any time. – kiss-o-matic Oct 10 '14 at 02:57

2 Answers2

1

You can use

.replace

str.replace(str.begin()+1,str.end()1,1,''); 

the reference

1

My approach would be:

  1. find the end of the first word: std::string maybe_article = input.substr(0, input.find_first_of(" \t"));
  2. check if it's an article: if (maybe_article == "a" or maybe_article == "the" or maybe_article == "the")
  3. if so, take the substring. return input.substring(input.substr(input.find_first_not_of(" \t", maybe_article.size())));
  4. otherwise, return just the original string. return input;

It's possible to avoid the allocation of maybe_article, but if you're restricting yourself to the standard library this is the most obvious way, and is easily modified to support a count in place of the article.

Personally I gave up on std::string and wrote my own.

o11c
  • 15,265
  • 4
  • 50
  • 75