【发布时间】:2014-05-13 00:40:23
【问题描述】:
我需要能够仅使用 c++ 字符串才能更改下面的输入段落。我遇到的问题是,当我在末尾拉出带有标点符号的东西时,例如“programs-”,它会将它作为“programs-”而不是“programs”和“-”分别拉入我的数组中。我需要知道如何执行此操作,以便可以将所有“-”替换为“.”。
有人如何将两者分开并将每个放在数组中自己的块中?
#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 意识到汤米不会那样做——结果是 当尼科尔森德斯做了萨姆本可以做的事时,汤米责怪山姆-
【问题讨论】:
-
我喜欢示例文本毫无意义。
-
你可以使用
isalpha函数来检查一个字符是否是字母。 -
示例文本应该没有意义,因为我将用不同的文本替换其中的文本。这包括将所有“-”更改为“。”尼尔,我不确定如何使用字符串并将其从文件中提取出来。