统计一段英文语句中单词的个数并按单词的长度进行排序

pigeon_2008 2008-05-15 07:04:00
请指教!麻烦给出思路!(最好有部分代码)
...全文
574 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pigeon_2008 2008-05-18
  • 打赏
  • 举报
回复
单词长度就是一个英文单词里有多少个字母,按这个进行排序。我说的有点模糊了,呵呵!
  • 打赏
  • 举报
回复
飞雪的代码不错……
楼主 按单词的长度进行排序 是什么意思?
长单词 个数
短单词 个数
???
还是:
长单词 短单词???
kingstarer 2008-05-15
  • 打赏
  • 举报
回复
首先将文本读进来,然后分词,存到二维字符数组(相当于字符串数组)

然后用冒泡法进行排序(要自己编写判断大小的函数和两个元素值互换的函数)
pigeon_2008 2008-05-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bargio_susie 的回复:]C++的语句结构不太懂哦,但谢谢您!
简单示例代码:


C/C++ code#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


class grt_cls
{
public:
bool operator () (const string &a, const string &b)
{
return(a.size() > b.size());
}
};


int main()
{
vector <string> svec;
string str;

while (cin >> str)
svec.push_back(str);


[/Quote]
pigeon_2008 2008-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baihacker 的回复:]首先表示谢谢,函数qsort()有带于研究,呵呵
C/C++ code#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* l, const void* r)
{
return strlen(*(char**)l) > strlen(*(char**)r);
}
int main(int argc, char* argv[])
{
char* data[] = {"abcde", "abcd", "a", "ab", "abc"};
int i;
qsort(data, sizeof(data)/sizeof(data[0]), sizeof(data[0]), cmp);
for (i = 0; i != sizeof(data)/sizeof(d…
[/Quote]
baihacker 2008-05-15
  • 打赏
  • 举报
回复
#include	<stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* l, const void* r)
{
return strlen(*(char**)l) > strlen(*(char**)r);
}
int main(int argc, char* argv[])
{
char* data[] = {"abcde", "abcd", "a", "ab", "abc"};
int i;
qsort(data, sizeof(data)/sizeof(data[0]), sizeof(data[0]), cmp);
for (i = 0; i != sizeof(data)/sizeof(data[0]); ++i)
printf("%s\n", data[i]);
return 0;
}
bargio_susie 2008-05-15
  • 打赏
  • 举报
回复
简单示例代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


class grt_cls
{
public:
bool operator () (const string &a, const string &b)
{
return(a.size() > b.size());
}
};


int main()
{
vector <string> svec;
string str;

while (cin >> str)
svec.push_back(str);

sort(svec.begin(), svec.end(), grt_cls() );

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

system("pause");
return 0;

}
chlaws 2008-05-15
  • 打赏
  • 举报
回复
用数组读取单个读取,fscanf(&buffer[i],"%s",fp);
当读到',','.'等标点符号则跳过不要存储.
读完后用strcmpi进行排序比较.
排完后再写入就OK了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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