0

単語を解析しているドキュメントがありますが、az、AZ、0-9、またはアポストロフィ以外のものはすべて空白と見なしたいと考えています。以前に次のコードを使用している場合、どうすればこれを行うことができますか:

ifstream file;
file.open(filePath);

while(file >> word){
    listOfWords.push_back(word); // I want to make sure only words with the stated 
                                 // range of characters exist in my list.
}

たとえば、hor.se という単語は、私のリストでは "hor" と "se" という 2 つの要素になります。

4

1 に答える 1

0

「空白文字」のリストを作成し、文字に遭遇するたびに、その文字がリストに含まれているかどうかを確認し、含まれている場合は新しい単語を開始しました. この例は python で書かれていますが、概念は同じです。

def get_words(whitespace_chars, string):
    words = []
    current_word = ""
    for x in range(0, len(string)):
        #check to see if we hit the end of a word.                                                                                                                                                                                           
        if(string[x] in whitespace_chars and current_word != ""):
            words.append(current_word)
            current_word = ""
        #add current letter to current word.                                                                                     
        else:
            current_word += string[x]
    #if the last letter isnt whitespace then the last word wont be added, so add here.                                                                                                                                                       
    if(current_word != ""):
        words.append(current_word)
    return words

言葉を返す

于 2013-01-27T03:05:46.233 に答える