2. C语言实现字符串倒置

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
char *src = “hello world”;
char dest = NULL;
int len = strlen(src);
dest = (char
)malloc(len+1);
char *d = dest;
char *s = &src[len-1];
while(len-- != 0) // 循环赋值进行len次
*d++ = s–; // 赋值结束后执行d++,此时指针指向len位置处
*d = 0; //字符串常量末尾为0,及指针len位置处值为0
printf("%s\n", dest);
free(dest);

return 0;

}

发布了43 篇原创文章 · 获赞 0 · 访问量 384

猜你喜欢

转载自blog.csdn.net/weixin_42505877/article/details/104181194