1

Given a string such as:

std::string word = "Hello World!it's@.-/sunday";

I want to extract all the words which are separated by non-alphabetical letters stored in a container like a vector, so for this case it would be:

Hello, World, it, s, sunday 

My initial try was to use isalpha() along with an index count to substr() accordingly like so:

std::string word = "Hello World!it's@.-/sunday";
    int count = 0, index = 0; 
    std::string temp;  
    
     for (; count < word.length();count++){
        if (!isalpha(word[count])){
            temp = word.substr(index,count);
            index = count; 
        }
    }

But this does not work as I thought it would as the index does not update fast enough resulting in words mixed with non-alphabetical characters. Is there perhaps a function or a better way to extract said words?

cigien
  • 57,834
  • 11
  • 73
  • 112

2 Answers2

1

You can try the following code. It constructs the words char by char.

#include <string>
#include <iostream>
#include <vector>
 
int main()
{
std::string phrase = "Hello World!it's@.-/sunday";
std::vector<std::string> words;  
std::string tmp;
 for (const char &c:phrase)
 {
        if (isalpha(c))
        {
            tmp.push_back(c);
        }
        else
        {
            words.push_back(tmp);
            tmp = "";
        }
 }
 if (!tmp.empty())
 {
     words.push_back(tmp);
 }
 
 for (const auto w : words)
 {
     std::cout << w << std::endl;
 }
 
 return 0;
}
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20
0

one easy way is that replace all the non-alphabetical char to space than split around space.

pradeexsu
  • 1,029
  • 1
  • 10
  • 27