#include <stdio.h>
#include "iostream"
#include <string.h>
int main()
{
char *src = "hello";
int len= strlen(src);
char *dest = (char *)malloc(len+1);
char *d= dest;
char *s = &src[len-1];
len = 1;
while (len--)
{
*d++=*s--;
}
*d = 0; //否则会乱码
printf("%s\n",dest);
free(dest);
return 0;
}
#include "iostream"
#include <string.h>
int main()
{
char *src = "hello";
int len= strlen(src);
char *dest = (char *)malloc(len+1);
char *d= dest;
char *s = &src[len-1];
len = 1;
while (len--)
{
*d++=*s--;
}
*d = 0; //否则会乱码
printf("%s\n",dest);
free(dest);
return 0;
}
采用数组的方法
#include <stdio.h>
#include "iostream"
#include <string.h>
int main()
{
char src[] = "hello";
int len = strlen(src);
char temp;
for (int i=0;i<len/2;i++)
{
temp = src[i];
src[i] = src[len-i-1];
src[len-i-1] = temp;
}
printf("%s\n",src);
return 0;
}
参考的代码#include "iostream"
#include <string.h>
int main()
{
char src[] = "hello";
int len = strlen(src);
char temp;
for (int i=0;i<len/2;i++)
{
temp = src[i];
src[i] = src[len-i-1];
src[len-i-1] = temp;
}
printf("%s\n",src);
return 0;
}
int main(){
char* src = "hello,world";
int len = strlen(src);
char* dest = (char*)malloc(len+1);//要为\0分配一个空间
char* d = dest;
char* s = &src[len-1];//指向最后一个字符
while( len-- != 0 )
*d++=*s--;
*d = 0;//尾部要加\0
printf("%s\n",dest);
free(dest);// 使用完,应当释放空间,以免造成内存汇泄露
return 0;
}
#include <stdio.h>
#include <string.h>
main()
{
char str[]="hello,world";
int len=strlen(str);
char t;
for(int i=0; i<len/2; i++)
{
t=str[i];
str[i]=str[len-i-1]; str[len-i-1]=t;
}
printf("%s",str);
return 0;
}
char* src = "hello,world";
int len = strlen(src);
char* dest = (char*)malloc(len+1);//要为\0分配一个空间
char* d = dest;
char* s = &src[len-1];//指向最后一个字符
while( len-- != 0 )
*d++=*s--;
*d = 0;//尾部要加\0
printf("%s\n",dest);
free(dest);// 使用完,应当释放空间,以免造成内存汇泄露
return 0;
}
#include <stdio.h>
#include <string.h>
main()
{
char str[]="hello,world";
int len=strlen(str);
char t;
for(int i=0; i<len/2; i++)
{
t=str[i];
str[i]=str[len-i-1]; str[len-i-1]=t;
}
printf("%s",str);
return 0;
}