简体   繁体   中英

Function to get String Word by Word

I am trying to write a function which gets a String, it will then look for the first word and return it, and then delete it from the Inputstring. This all works but the problem I am facing is that once there are more than one whitespace it starts deleting letters from the next word in the string, which i dont want because i might need to check the second word for its properties too by calling the function again.

std::string extractWord (std::string& inspectThis)
{
  std::string firstWord;
  int i = 0;

  for (int count = 0; count < inspectThis.length(); count++) {
    if (isalpha(inspectThis[count]))
      firstWord += inspectThis[count];
    else if (firstWord.length() > 0)
      break;
  }
  int pos = inspectThis.find(firstWord);
  inspectThis.erase(pos, pos + firstWord.length());

  return firstWord;
}



int main() {
  std::string name = "   Help  Final  Test";
  std::cout<<extractWord(name) << std::endl;
  std::cout<<extractWord(name) << std::endl;
  std::cout<<extractWord(name) << std::endl;

  return 0;
}

When I test my function like this the output will be: "Help inal est"

The title to this post is

Function to get String Word by Word

Becuase all of this can be done with one statement, a separate function is not necessary. All this many lines of code can be replaced by using modern C++ language elements and the standard library.

Please see:

#include <iostream>
#include <string>
#include <regex>
#include <algorithm>
#include <iterator>

std::regex word{ R"(\w+)" };


int main() {

    // The string
    std::string name = "   Help  Final  Test";

    // Copy words to std::cout
    std::copy(std::sregex_token_iterator(name.begin(), name.end(), word), {}, std::ostream_iterator<std::string>(std::cout, "\n"));

    return 0;
}

Because of the simplicty of the program (one line of code), I am not so sure, what to explain. So, we use std::copy to copy something between a source iterator pair to a destination iterator. The destination iterator is the std::ostream_iterator , which will (very simplified) call the inserter function << for each source element.

The source element is the std::sregex_token_iterater which has been designed to iterate over elements in a string that follow a certain pattern. The pattern is \w+, which means, one or more alphanum characters.

If you have questions, I am happy to answer.

You could use std::istringstream and not be concerned how many spaces there are:

#include <string>
#include <cctype>
#include <sstream>
#include <iostream>

std::string extractWord(std::string& inspectThis)
{
    std::istringstream strm(inspectThis);
    std::string word;
    std::string first_word;
    while (strm >> word)
    {
        if (isalpha(word[0]))
        {
            // This is the first word
            first_word = word;
            std::string newString;

            // get the rest of the words and reset the inspectThis
            while (strm >> word)
                newString += word + " ";
            inspectThis = newString;
            return first_word;
        }
    }
    return "";
}

int main() 
{
    std::string name = "   Help  Final  Test";
    std::cout << extractWord(name) << std::endl;
    std::cout << extractWord(name) << std::endl;
    std::cout << extractWord(name) << std::endl;
}

Output:

Help
Final
Test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM