现在的位置: 首页 > 综合 > 正文

int reverseSentence(char** sentence, char ** newsentance, int maxWords)

2013年08月13日 ⁄ 综合 ⁄ 共 773字 ⁄ 字号 评论关闭

根据一个百度问答写的一个程序:特点是用指针实现。。

http://zhidao.baidu.com/question/485670365.html?sort=6&old=1&afterAnswer=1#reply-box-1218524351

#include "stdafx.h"
#include<iostream>
#include <cctype>
using namespace std;
#define MAXLEN 20
int reverseSentence(char** sentence, char ** newsentance, int maxWords)
{
    int wcnt = 0;
    int len = strlen(*sentence);
    char words[MAXLEN];
    char * p = words;
    char * q = *newsentance + len;
	char *r = *sentence;
    *q = '\0';
    q --;
    while(1)
    {
                    if(!isalpha(*r))
                    {
                                 if(isalpha(*(r -1)))wcnt ++;
								 p --;
                                 while(p >= words)
                                 {
                                        *q = *p;
                                        q --;
                                        p --;                                          
                                 }
                                 if(*r)
                                 {
                                              *q = *r;
											   q --;
											   r ++;
											   p = words;
											   continue;
                                 }
                                 else break;                                 
                    }
                    *p = *r;
                    p ++;
                    r ++;
    }
    return wcnt;
} 
int main()
{
    char *s = new char[100];
    char *s2 = new char[100];
	cout << "please input a sentence :" << endl;
	cin.getline(s,100);
   cout << "there are " <<  reverseSentence(&s,&s2,100) << " words in the sentence" << endl;
    cout << s2 << endl;
    system("pause");
    return 0;
    }

抱歉!评论已关闭.