编程实现字符串中各单词的翻转

编写函数,将"i am from henan "倒置为"henan from am i"即将句子中的单词位置倒置,而不改变单词内部的机构

 编程实现字符串中各单词的翻转

第一种方法:

 

#include "stdafx.h"
#include 
using namespace std;
//编程实现字符串中各单词的翻转
//方法1
void Revese(char *str){

	char *start=str,*end=str,*ptr=str; //开头,结尾,中间指针
	while (*ptr++!='\0')
	{
		if (*ptr==' '||*ptr=='\0') //找到一个单词
		{
			end=ptr-1; //end指向单词末尾
			while (start


方法1  先把每个单词逆置,再将整个字符串逆置

#include "stdafx.h"
#include 
using namespace std;
//编程实现字符串中各单词的翻转
//方法2
void Revese(char *str){
	char *start=str,*end=str,*ptr=str;
	while (*ptr++!='\0');
	end=ptr-2; //找到字符串末尾
	while (start


方法2 先将整个字符串逆置,再将每个单词逆置

你可能感兴趣的:(面试题集)