2013年(56)
分类: C/C++
2013-11-05 11:26:51
废话不多说,我们直接上代码:
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
void reverse(char * ps, char * pn);
char * strprocess(char * str);
char * strCopy(char * dst, const char * src);
int main(int argc, char * argv[])
{
char str[] = "could you tell me how to complete this problem";
char * p = strprocess(str);
cout<
char * p2 = (char *)malloc(strlen(str));
p2 = strCopy(p2, str);
cout<
system("pause");
return 0;
}
char * strprocess(char * str)
{
if(str == NULL || *str == '\0')
return NULL;
char * ps = str;
char * pn = str;
while(*pn != '\0') pn++;
pn--;
reverse(ps, pn);
ps = str;
while(*ps != '\0')
{
pn = ps;
while(*pn != '\0' && *pn != ' ' ) pn++;
reverse(ps, pn - 1);
if(*pn == '\0') break;
ps = pn + 1;
}
return str;
}
void reverse(char * ps, char * pn)
{
while(ps <= pn)
{
char temp = *ps;
*ps = *pn;
*pn = temp;
ps++;
pn--;
}
}
// copy str
char * strCopy(char * dst, const char * src)
{
if(dst == NULL || src == NULL) return NULL;
char * strDest = dst;
while((*dst++ = *src++)!='\0');
return strDest;
}