【发布时间】:2011-04-09 13:20:32
【问题描述】:
我对编程有点陌生,所以我的问题的答案并不明显,尽管我尝试了多种方式来完成这项任务。
问题是我试图获取一个单词数组,从数组中删除任何标点符号,然后将新单词放入一个单独的数组中。我试图这样做,但是当我输出新数组时,我不断收到垃圾值。
代码如下:
norm(sepwords1,sepwords2,numwords); <- where I called it in main
void norm(string words[], string wordz[],int count)
{
int i;
int x;
string newstring="";
char current;
for(i=0; i<count; i++)
{
for(x=0; x<words[i].length();x++)
{
current= words[i].at(x);
if(ispunct(current)==0)
{
newstring += current;
}
}
wordz[i]= newstring;
}
}
完整的主要功能是:
int main (int argc, char* argv[])
{
int count = argc;
int i;
string filename[count];
ifstream infile;
string fromfile[1000];
int numdata;
int pass;
char current;
int sum;
string masterstring="";
int x;
string sepwords[2000];
int sum1;
string temp="";
int start;
int fin;
string newstring="";
string newfile[1000];
int place;
int numwords;
string sepwords1[2000];
string newmaster="";
int j=0;
string currentz;
string highmark;
int index[2000];
string sepwords2[2000];
int counta=0;
for(i=0; i < count-1; i++)
{
filename[i] = argv[i+1];
}
for( i=0; i < count-1; i++)
{
infile.open(filename[i].c_str());
numdata=0;
while(!infile.eof())
{
getline(infile, fromfile[numdata], '\n');
numdata++;
}
for(i=0; i<numdata; i++)
{
cout<<fromfile[i]<<endl;
masterstring += fromfile[i] + " "; //NUMBER ONE
}
numwords = split(masterstring, sepwords);
cout<<numwords<<endl; //NUMBER TWO
}
for(i=0;i<numwords;i++)
{
newstring = toupper(sepwords[i].at(0));
newstring += sepwords[i].substr(1);
sepwords1[i] = newstring;
newstring="";
}
for(i=0;i<numwords;i++)
{
newmaster += sepwords1[i] + " ";
j++;
if(j > 10)
{
newmaster+= '\n';
j=0;
}
}
cout<<newmaster<<endl; //NUMBER THREE
norm(sepwords1,sepwords2,numwords);
for(i=0;i<numwords;i++)
{
cout<<sepwords2<<endl;
}
return 0;
}
【问题讨论】:
-
如何声明传递给
norm的原始数组?你有什么理由使用数组而不是std::vector,因为我认为这个字符串列表可能是可变的? -
我想你想 newstring="" 为外循环的每次迭代。
-
我没有使用矢量只是因为我还没有知道如何实现一个:P
-
@Sam,
<vector>是 C++ 标准库的一部分。如果这是家庭作业,那么我可以理解……您能否也发布完整的main功能? -
这一行是错误的“cout