#include <string>
#include "stdafx.h"
void Reverse(char *pBegin, char *pEnd)
{
if(pBegin == NULL || pEnd == NULL)
return;
while(pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
pBegin ++, pEnd --;
}
}
char* ReverseSentence(char *pData)
{
if(pData == NULL)
return NULL;
char *pBegin = pData;
char *pEnd = pData;
while(*pEnd != '\0')
pEnd ++;
pEnd--;
// 翻转整个句子
Reverse(pBegin, pEnd);
// 翻转句子中的每个单词
pBegin = pEnd = pData;
while(*pBegin != '\0')
{
if(*pBegin == ' ')
{
pBegin ++;
pEnd ++;
}
else if(*pEnd == ' ' || *pEnd == '\0')
{
Reverse(pBegin, --pEnd);
pBegin = ++pEnd;
}
else
{
pEnd ++;
}
}
return pData;
}
int main()
{
char input[] = "I am a student.";
printf("%s\n\n",input);
printf("After reverse.\n\n");
ReverseSentence(input);
printf("%s\n", input);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50001], ch;
int i, low, high, tmp, len;
while( gets( str ) )
{
low = 0;
high = 0;
len = strlen( str );
while( low < len )
{
while( str[low] == ' ' )
{
low++;
}
high = low;
while( str[high] )
{
if( str[high] == ' ' )
{
high--;
break;
}
else
{
high++;
}
}
if( str[high] == '\0' )
{
high--;
}
tmp = high + 1;
while( low < high )
{
ch = str[low];
str[low] = str[high];
str[high] = ch;
low++;
high--;
}
low = tmp;
high = tmp;
}
for( i = len - 1; i > 0; i-- )
{
printf("%c", str[i]);
}
printf("%c\n", str[0]);
}
return 0;
}
#include <iostream>
using namespace std;
void reverse_part(char*,int pBegin,int pEnd);
void reverse(char *str)
{
//n为字符串长度
int n=strlen(str)-1;
reverse_part(str,0,n);
int pBegin=0,pEnd=0;
while(str[pEnd+1]){
if(str[pEnd]!=' ' && str[pEnd]!='\0')
++pEnd;
//找到空格
else{
reverse_part(str,pBegin,pEnd-1);
//如果下一个还是空格
while(str[pEnd+1]!='\0' && str[pEnd+1]==' ')
++pEnd;
pBegin=++pEnd;
}
}
cout<<str<<endl;
}
void reverse_part(char *str,int pBegin,int pEnd)
{
char temp;
for(int i=pBegin;i<=(pEnd-pBegin)/2;++i){
temp=str[i];
str[i]=str[pEnd-i];
str[pEnd-i]=temp;
}
}
void main()
{
char str[]="I am a student.";
reverse(str);
system("pause");
}
#include <iostream>
using namespace std;
void reverse_part(char*,int pBegin,int pEnd);
void reverse(char *str)
{
//n为字符串长度
int n=strlen(str)-1;
reverse_part(str,0,n);
int pBegin=0,pEnd=0;
while(str[pEnd+1]){
if(str[pEnd]!=' ' && str[pEnd]!='\0')
++pEnd;
//找到空格
else{
reverse_part(str,pBegin,pEnd-1);
//如果下一个还是空格
while(str[pEnd+1]!='\0' && str[pEnd+1]==' ')
++pEnd;
pBegin=++pEnd;
}
}
cout<<str<<endl;
}
void reverse_part(char *str,int pBegin,int pEnd)
{
char temp;
for(int i=pBegin;i<=(pEnd-pBegin)/2;++i){
temp=str[i];
str[i]=str[pEnd-i];
str[pEnd-i]=temp;
}
}
void main()
{
char str[]="I am a student.";
reverse(str);
system("pause");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有