新手求教.如何将一篇文章中的英文单词,存放在一个数组中?

opponent2012 2011-12-30 08:54:24
比如一篇文章:In the Orient young bulls are tested for the fight arena in a certain manner. Each is brought to the ring and allowed to attack a picador who pricks them with a lance. The bravery of each bull is then rated with care according to the number of times he demonstrates his willingness to charge in spite of the sting of the blade. Henceforth will I recognize that each day I am tested by life in like manner. If I persist, if I continue to try, if I continue to charge forward, I will succeed.
如何将上面这篇文章中的单词取出来,存放在一个数组中?唉,基础太差。
...全文
636 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiangXiang 2011-12-31
  • 打赏
  • 举报
回复
排了下版
把你要处理的存入文件text
我在linux g++下运行成功

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string> result;
ifstream inFile("text");

if(inFile == 0)
{
cout << "fail to open file text" << endl;
return 1;
}

string word;
string temp;
while(!inFile.eof())
{
getline(inFile, temp);

for(int k = 0; k < temp.size(); k++)
{

if((temp[k] >= 'A' && temp[k] <= 'Z') || (temp[k] >= 'a' && temp[k] <= 'z'))
word += temp[k];
else
if(!word.empty())
{
result.push_back(word);
word = "";
}
}

}

for(vector<string>::iterator iter = result.begin(); iter != result.end(); ++iter)
cout << *iter << endl;


return 0;
}
JiangXiang 2011-12-31
  • 打赏
  • 举报
回复
这样可以。
把你要处理的不能进文件text
我在linux g++下运行成功

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string> result;
ifstream inFile("text");

if(inFile == 0)
{
cout << "fail to open file text" << endl;
return 1;
}

string word;
string temp;
while(!inFile.eof())
{
getline(inFile, temp);

for(int k = 0; k < temp.size(); k++)
{

if((temp[k] >= 'A' && temp[k] <= 'Z') || (temp[k] >= 'a' && temp[k] <= 'z'))
word += temp[k];
else
if(!word.empty())
{
result.push_back(word);
word = "";
}
}

}

for(vector<string>::iterator iter = result.begin(); iter != result.end(); ++iter)
cout << *iter << endl;


return 0;
}
opponent2012 2011-12-31
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 johnpher 的回复:]

排了下版
把你要处理的存入文件text
我在linux g++下运行成功
C/C++ code

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string> result;
ifst……
[/Quote]

#include<iostream>
#include <fstream>
using namespace std;
char str[]="hello this is a c programe!n"
"i know this difference n"
"and mocive";
ofstream outfile ("data.txt");
for(int i=0;i<strlen(str);i)
outfile.put(str[i]);

帮忙看下这个程序,for(int i=0;i<strlen(str);i)这句话哪错了?错误提示:
g++ -Wall -c "练习3.cpp" (在目录 /home/zhaoyu/文档 中)
练习3.cpp:8:1: 错误: expected unqualified-id before ‘for’
练习3.cpp:8:13: 错误: ‘i’不是一个类型名
练习3.cpp:8:27: 错误: ‘i’不是一个类型名
编译失败。
opponent2012 2011-12-31
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 johnpher 的回复:]
楼主是不是有点不厚道阿 2011的帖子不要拖到2012结嘛

C/C++ code

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

using namespace std;

char str[][50] = {"hello this is a c programe!n", "i know this differ……
[/Quote]
oh my god.不是有意的。
JiangXiang 2011-12-31
  • 打赏
  • 举报
回复
楼主是不是有点不厚道阿 2011的帖子不要拖到2012结嘛


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

using namespace std;

char str[][50] = {"hello this is a c programe!n", "i know this difference n", "and mocive"};

int main()
{
ofstream outfile ("data.txt");
for(int k = 0; k < 3; k++)
for(int i=0;i< strlen(str[k]);i++)
outfile.put(str[k][i]);
outfile.close();
return 0;
}
JiangXiang 2011-12-31
  • 打赏
  • 举报
回复
二位数组不能这样定义阿
opponent2012 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 johnpher 的回复:]

5楼的代码是从控制台读取的输入阿,怎么会有问题?
[/Quote]
那代码是我写的,我测试了下 确实有问题,首先最后一个单词放不进数组,其次for是个无限循环。不知道咋修改才好?
JiangXiang 2011-12-30
  • 打赏
  • 举报
回复
5楼的代码是从控制台读取的输入阿,怎么会有问题?
lucky-lucky 2011-12-30
  • 打赏
  • 举报
回复
boost Tokenizer试试
opponent2012 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 johnpher 的回复:]

i不是计数器吗?
什么 叫 最后一个单词读不出来
[/Quote]
按5楼的程序显示不了最后一个单词,i虽然是计数器,但是上面的那个程序好像问题不少,for循环可能是无限循环?
JiangXiang 2011-12-30
  • 打赏
  • 举报
回复
i不是计数器吗?
什么 叫 最后一个单词读不出来
opponent2012 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 johnpher 的回复:]

getline可以
[/Quote]

#include<iostream>
#include<string>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void)
{
string str[10000];
int i=0;
for(;;i++)
{
getline(cin,str[i],' ');
cout<<str[i]<<endl;
}
return 1;
}

这种方法,就是最后一个单词读不出来,还有就是不知道数组的项数,不知道这种方法可不可以改进?
JiangXiang 2011-12-30
  • 打赏
  • 举报
回复
getline可以
opponent2012 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 whoami1978 的回复:]

网上搜下,这个很多的,CSDN中就有很多这样的作业题
[/Quote]
我没老师,没作业。问一个问题,C++中什么函数读取字符串的时候会连带空格一起读,直到遇到换行符和EOF结束。我知道在C中gets可以,不知道C++里有吗?cin肯定不行。
#include<iostream>
#include<stdio.h>
using std::cout;
using std::cin;
using std::endl;
int main(void)
{
char word[10000],*ptr[1000]={NULL};
int i=0,j=0;
cout<<"Please input a string:"<<endl;
gets(word);
cout<<"The result is:"<<endl;
while(word[i]==' ')
i++;
ptr[j]=&word[i];
j++;
while(word[i])
{
if(word[i]==' ')
{
word[i]='\0';
if(word[i+1]&&word[i+1]!=' ')
{
ptr[j]=&word[i+1];
j++;
}
}
i++;
}
for(i=0;i<j;i++)
cout<<ptr[i]<<endl;
}
whoami1978 2011-12-30
  • 打赏
  • 举报
回复
网上搜下,这个很多的,CSDN中就有很多这样的作业题
yafeng_jiang 2011-12-30
  • 打赏
  • 举报
回复
strtok函数

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧