一、概述

  案例:使用C语言实现字符串的逆序,用数组和指针两种方式实现。

二、代码示例

#include <iostream>
#include <string.h>

using namespace std;

//字符串逆序,数组实现
void reserveStr(char *src,char* result){
	size_t count = strlen(src)-1;
	int j = 0;
	for(int i = count;i>=0;i--){
		result[j] = src[i];
		cout << "result:"<<result[j]<<" src:"<<src[i]<<endl;
		j++;
	}
	result[j]='\0';
}
//字符串逆序,指针实现
void reserveStr2(char* src ,char* result){
	char * start = src;
	char *end = src+strlen(src)-1;
	while(end>=start){
		*result = *end;
		result++;
		end--;
	}
}

int main(int argc, char const *argv[])
{
	char * src =(char*)"my babay is luoluoyang";
	char* result;
	cout <<"src:"<<src<<endl;
	reserveStr2(src,result);
	cout <<"result:"<<result<<endl;
	return 0;
}