2014-04-02 103 views
1

我需要只能使用C++字符串来改变下面的输入段落。我遇到的问题是,当我在最后插入标点符号的时候,比如“程序”,它将它分别作为“程序”而不是“程序”和“ - ”分别放入我的数组中。我需要知道如何做到这一点,以便我可以用“。”替换所有的“ - ”。C++字符串,标点符号,数组

某人如何区分这两者并将它们分别放置在阵列中自己的块中?

#include <iostream> 
#include <fstream> 

using namespace std; 


void read(string line[], string para[], int &d) 
{ 
string temp; 
int i = 0; 

ifstream myfile ("paragraphwordsub.txt"); 
if (myfile.is_open()) 
{ 
    while (temp != "$") 
    { 
     myfile >> temp; 
     line [i] = temp; 
     cout << line[i] << " "; 
     i++; 
    } 
    cout << endl; 
    i=0; 
    while (!myfile.eof()) 
    { 
     myfile >> temp; 
     para[i] = temp; 
     cout << para[i] << " "; 
     d++; 
     i++; 
    } 

myfile.close(); 
} 

else cout << "Unable to open file"; 

cout << endl << endl << i << endl << para[73] << endl; 

return; 

} 

int main() 
{ 
    int const SIZE = 100; 
    string a [SIZE]; 
    string b [SIZE]; 
    int counter = 0; 

    read(a, b, counter); 

    cout << endl << endl << counter << endl; 


    return 0; 

} 

输入:

谁的程序是这样的? 有重要的节目要做,汤米被要求做这样的 - 汤米相信山姆会这样做 - 参孙可以做到这一点,但尼科尔森这样做 - 山姆对此感到愤怒,因为 这样的汤米的节目 - 汤米以为参孙可以做到这样,但 Nicholsonnders意识到,汤米不会做such-它结束了那 汤米指责山姆时Nicholsonnders做了什么参孙可以有私下作出─

+2

我喜欢示例文本没有意义。 –

+0

你可以使用'isalpha'函数来检查一个字符是否是字母。 –

+0

示例文本应该是没有意义的,因为我将用不同的文本替换其中的文本。这包括将所有“ - ”改为“”。尼尔,我不知道我会如何使用字符串并从文件中拉出它。 – Limit

回答

0

我并不完全明白你想要什么用它做,但这里是一个如何标记段落,同时保持标点符号和空格分开的例子。我使用的载体,而不是数组,但它应该很容易切换:

#include <vector> 
#include <fstream> 
#include <ctype.h> 
#include <iostream> 
#include <string> 

class Words { 
    std::vector<std::string> words_; 
    bool lastCharacterIsAlphaNumeric_; 

    void addAlphaNumeric(char character); 
    void startNewWord(char character) { words_.emplace_back(1, character); } 
    void addNewWordForNonAlphaNumeric(char character); 
    void clear() { words_.clear(); lastCharacterIsAlphaNumeric_ = false; } 

public: 
    Words() : lastCharacterIsAlphaNumeric_(false) {} 
    void read(std::string filename); 
    void print() { for(auto& word : words_) std::cout << word << std::endl; } 
}; 

void Words::addAlphaNumeric(char character) { 
    if (!lastCharacterIsAlphaNumeric_) { 
    startNewWord(character); 
    lastCharacterIsAlphaNumeric_ = true; 
    } else { 
    words_.back().push_back(character); 
    } 
} 

void Words::addNewWordForNonAlphaNumeric(char character) { 
    startNewWord(character); 
    lastCharacterIsAlphaNumeric_ = false; 
} 

void Words::read(std::string filename) { 
    clear(); 
    std::ifstream myfile (filename); 
    char tmp; 

    while(myfile.good()) { 
    myfile.get(tmp); 
    if(isalnum(tmp)) { 
     addAlphaNumeric(tmp); 
    } else { 
     addNewWordForNonAlphaNumeric(tmp); 
    } 
    } 
} 

int main() 
{ 
    Words words; 
    words.read("paragraphwordsub.txt"); 
    words.print(); 
} 
0

如果我理解正确的问题,你要查找的字符串的每次出现“ - ”在你的输入,并与更换子串“。”。

您可能一直希望,因为操作员>>一次从输入中读取一个“单词”,它将在“程序”的最后一个字母后停止;但实际上它只在涉及空白或输入结束时停止。看起来最好有一个流操纵器可以用来告诉它停在标点符号上,但我不知道任何这样的操纵器。

你可以尝试这样的事:

myfile >> temp; 
size_t position = temp.find("-"); 
while (found!=std::string::npos) { 
    temp.replace(position, 1, "."); 
} 

如果使用字符串变量而不是文字“ - ”和,你可以概括这让你想要的任何替代(但随后而不是“”参数replace()中的常量1,您必须使用子字符串的长度进行替换)。