文章中单词个数统计 字符串以空格反转

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;
}
我自己写的,太麻烦了.可以省一个链表的,改天要改一下.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值