将一句话里面的单词进行倒置,标点符号不倒换,比如一句话"i come from tianjing." 倒换后变为tianjin. from come i

void Reverse(char *p,char *q)
{
    while(p<q)
    {
        swap(*p,*q);
        p++;
        q--;
    }
}
void main()
{

    char s[]="i come from tianjin.";
    char *p=s,*q=s+strlen(s)-1;
    Reverse(p,q);
    p=s;
    q=s;
    while(true)
    {
        if(*q==' ')
        {
            Reverse(p,q-1);
            p=q;
            while(*p!='\0'&&*p==' ')
                p++;
            if(*p=='\0')
                break;
            q=p;
        }
        if(*q=='\0')
        {
            Reverse(p,q-1);
            break;
        }
        q++;
    }

    cout<<"转换后:"<<s<<endl;


    system("pause");
}

 

posted on 2013-07-18 10:40  dyc0113  阅读(1337)  评论(0编辑  收藏  举报

导航