// reversestring.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include "iostream"
using namespace std;
int strlen1(char *string)
{
int length=0;
while(*string++!='\0')
length+=1;
return length;
}
void reverse_string(char *string)
{
int str_len=strlen1(string);
char *reversestring; //切记指针初始化
reversestring=(char *)malloc(str_len*sizeof(char)); //动态分配内存,使用强制转换类型,malloc是void*类型的
for (int i=0;i<str_len;i++)
{
*(reversestring+i)=*(string+str_len-1-i);
printf("%c",*(reversestring+i));
}
printf("\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str = "ABCDEFG";
cout<<"翻转后的字符串:"<<endl;
reverse_string( str);
system("pause");
return 0;
}
09-01