文章中单词个数统计 字符串以空格反转
2005-04-15 11:00
555 查看
1、有一篇文章存储在数组中统计出现的单词并按照出现次数排序
2、将一个存储在数组中的英文句子的单词倒排例如“my name is laolaoliu2002"变成”laolaoliu2002 is name my“不要使用额外的数组存储空间
是一个朋友的问题由于我最近比较忙(要出差)没有时间写了。
#include<iostream>
using namespace std;
char* change(char *str)
{
char *p=str,*q=str,*str1,*q1;
int len=strlen(str)-1;
str1=new char[len+2];
//if(!str1)cout<<"ERROR!"<<endl;
while(*p)
{
if(*p!=' ')
{
if(*q==' '||*q==0)
{
q1=q;
do
{
str1[len]=*(--q);
len--;
}while(p!=q);
p=q=q1;
}
else q++;
}
else
{
str1[len]=' ';len--;p++;q++;
}
}
str1[strlen(str)]=*p;
return str1;
}
int main()
{
cout<<"请输入你要转换的字符串!"<<endl;
char str[80];
cin.getline(str,80);
cout<<change(str)<<endl;
system("PAUSE");
return 0;
}
我自己写的.
#include<iostream>
using namespace std;
char *rev(char *src)
{
char *b, *e,ch;
b = src;
strrev(src);
while (*b)
{
while (isspace(*b))
++b;
e = b;
while (*e && !isspace(*e))
++e;
ch = *e,*e = '/0';
strrev(b);
b = e,*e = ch;
}
return src;
}
int main()
{
char a[80];
cout<<"请输入字符串"<<endl;
cin.getline(a,80);
cout<<rev(a)<<endl;
system("PAUSE");
return 0;
}
一个网友写的.很好.收下了.
=====================================================================
#include<iostream>
using namespace std;
struct node{
char *s;
int num;
node *next;
};
node* orderInsertnode(node *&head,node *p) //排序
{
node *p1,*p2;
if(!head) //建头
{
head=p;
p->next=0;
return head;
}
if(head->num>=p->num) //插入头之前
{
p->next=head;
head=p;
return head;
}
p1=p2=head;
while(p2->next&&p2->num<=p->num)
{
p1=p2;p2=p2->next;
}
if(p2->num<=p->num) //插入中间
{
p2->next=p;
p->next=0;
}
else //插入尾.
{
p->next=p2;
p1->next=p;
}
return head;
}
node *Insertnode(node *&head,node *p) //构建链表
{
node *q,*q1;
if(!head) //建头
{
head=p;
p->next=0;
p->num=1;
return head;
}
q1=q=head;
while(q) //处理中间情况
{
if(strcmp(q->s,p->s))
{
q1=q;q=q->next;
}
else
{
q->num+=1;
return head;
}
}
if(!q)q1->next=p; //插入尾部
return head;
}
node *filespik(char *file)
{
char *p=file,*q=p,*q1,e;
node *head=0,*head1=0,*word;
while(*p)
{
while(!isalpha(*p))++p; //指向单词开始处
q1=p;
while(isalpha(*q1)&&*q1)++q1; //指向单词尾处
e=*q1; //留COPY
*q1='/0'; //置结尾符
word=new node;
word->s=new char[strlen(p)+1];
strcpy(word->s,p); //放入单词
word->num=1;
word->next=0;
head=Insertnode(head,word); //插成无序表
word=0;p=q1; *p=e;
} //完成循环后所有单词被无序插入head中.
node *hh=head;
while(hh)
{
word=new node;
word->s=new char[strlen(hh->s)+1];
strcpy(word->s,hh->s);
word->num=hh->num;
word->next=0;
head1=orderInsertnode(head1,word);
hh=hh->next;
} //完成排序
while(head) //回收空间
{
hh=head;
head=hh->next;
delete hh->s;
delete hh;
}
return head1;
}
int main()
{
char test[]="aa aa bb bb bb bb test i love you but do you love me? so big disgrace! good luck";
node *result=filespik(test);
node *p=result;
while(p)
{
cout<<p->s<<" "<<p->num<<endl;
p=p->next;
}
while(result)
{
p=result;
result=result->next;
delete p->s;
delete p;
}
system("PAUSE");
return 0;
}
我自己写的,太麻烦了.可以省一个链表的,改天要改一下.
2、将一个存储在数组中的英文句子的单词倒排例如“my name is laolaoliu2002"变成”laolaoliu2002 is name my“不要使用额外的数组存储空间
是一个朋友的问题由于我最近比较忙(要出差)没有时间写了。
#include<iostream>
using namespace std;
char* change(char *str)
{
char *p=str,*q=str,*str1,*q1;
int len=strlen(str)-1;
str1=new char[len+2];
//if(!str1)cout<<"ERROR!"<<endl;
while(*p)
{
if(*p!=' ')
{
if(*q==' '||*q==0)
{
q1=q;
do
{
str1[len]=*(--q);
len--;
}while(p!=q);
p=q=q1;
}
else q++;
}
else
{
str1[len]=' ';len--;p++;q++;
}
}
str1[strlen(str)]=*p;
return str1;
}
int main()
{
cout<<"请输入你要转换的字符串!"<<endl;
char str[80];
cin.getline(str,80);
cout<<change(str)<<endl;
system("PAUSE");
return 0;
}
我自己写的.
#include<iostream>
using namespace std;
char *rev(char *src)
{
char *b, *e,ch;
b = src;
strrev(src);
while (*b)
{
while (isspace(*b))
++b;
e = b;
while (*e && !isspace(*e))
++e;
ch = *e,*e = '/0';
strrev(b);
b = e,*e = ch;
}
return src;
}
int main()
{
char a[80];
cout<<"请输入字符串"<<endl;
cin.getline(a,80);
cout<<rev(a)<<endl;
system("PAUSE");
return 0;
}
一个网友写的.很好.收下了.
=====================================================================
#include<iostream>
using namespace std;
struct node{
char *s;
int num;
node *next;
};
node* orderInsertnode(node *&head,node *p) //排序
{
node *p1,*p2;
if(!head) //建头
{
head=p;
p->next=0;
return head;
}
if(head->num>=p->num) //插入头之前
{
p->next=head;
head=p;
return head;
}
p1=p2=head;
while(p2->next&&p2->num<=p->num)
{
p1=p2;p2=p2->next;
}
if(p2->num<=p->num) //插入中间
{
p2->next=p;
p->next=0;
}
else //插入尾.
{
p->next=p2;
p1->next=p;
}
return head;
}
node *Insertnode(node *&head,node *p) //构建链表
{
node *q,*q1;
if(!head) //建头
{
head=p;
p->next=0;
p->num=1;
return head;
}
q1=q=head;
while(q) //处理中间情况
{
if(strcmp(q->s,p->s))
{
q1=q;q=q->next;
}
else
{
q->num+=1;
return head;
}
}
if(!q)q1->next=p; //插入尾部
return head;
}
node *filespik(char *file)
{
char *p=file,*q=p,*q1,e;
node *head=0,*head1=0,*word;
while(*p)
{
while(!isalpha(*p))++p; //指向单词开始处
q1=p;
while(isalpha(*q1)&&*q1)++q1; //指向单词尾处
e=*q1; //留COPY
*q1='/0'; //置结尾符
word=new node;
word->s=new char[strlen(p)+1];
strcpy(word->s,p); //放入单词
word->num=1;
word->next=0;
head=Insertnode(head,word); //插成无序表
word=0;p=q1; *p=e;
} //完成循环后所有单词被无序插入head中.
node *hh=head;
while(hh)
{
word=new node;
word->s=new char[strlen(hh->s)+1];
strcpy(word->s,hh->s);
word->num=hh->num;
word->next=0;
head1=orderInsertnode(head1,word);
hh=hh->next;
} //完成排序
while(head) //回收空间
{
hh=head;
head=hh->next;
delete hh->s;
delete hh;
}
return head1;
}
int main()
{
char test[]="aa aa bb bb bb bb test i love you but do you love me? so big disgrace! good luck";
node *result=filespik(test);
node *p=result;
while(p)
{
cout<<p->s<<" "<<p->num<<endl;
p=p->next;
}
while(result)
{
p=result;
result=result->next;
delete p->s;
delete p;
}
system("PAUSE");
return 0;
}
我自己写的,太麻烦了.可以省一个链表的,改天要改一下.
相关文章推荐
- 试编写一个程序,输入一个字符串,统计其中有多少各单词?单词之间用空格分开
- 用函数统计字符串的单词个数 空格数 字母数
- 字符串反转(按单词正序输出),保留并打印所有空格。
- C语言练习题每练 1:字符串统计单词个数,单词由空格隔开
- python---字符串的拼接、去重、反转、字母花样排序、单词出现判断、统计文件特定单词频率lambda、硬盘容量、列表转字符串
- 关于字符串的运用(去除空格,反转,统计次数,最长公共子串)
- 字符串处理:下划线,多空格变单空格,统计单词出现个数
- 输入一行字符串,统计其中有多少个单词,单词之间用一个或者多个空格分隔开
- 字符串数组反转输出 以空格为单词分隔符 C++
- 统计英文文章中空格数,单词数,字符数,并删除指定字符 (c语言)
- python---在文本/文章中统计字符串中出现单词次数
- 一没考虑单词重复,二没考虑先输入空格,三没考虑后面连续输空格,方法见空格将空格前面的当成一个字符串,字符串比较是否相同,再统计
- 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
- python统计文本字符串里单词出现频率的方法
- 统计英文文章中的字符串个数
- 将字符串中的每个单词顺序进行颠倒,单词还是原来的单词,字母顺序不发生变化,将字符串中的空格用 代替
- 统计传入字符串参数(可能不止一个参数)的英文字母、空格、数字和其他字符的个数
- trie学习 --HDU1251 输入字符串,统计以这个字符串为前缀的单词数量
- c语言统计从文件读取的文章中所有单词的出现次数
- 统计文章单词[JAVA实现]-经典笔试题