/*
用指针的方法,将字符串“ABCD1234efgh”前后对调显示
用指针的方法,将字符串“ABCD1234efgh”前后对调显示
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
int main()
{
char str[] = "ABCD1234efgh";
int length = strlen(str);
char *p1, *p2;
p1 = str;
p2 = str + length - 1;
while (p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
p1++;
p2--;
}
printf("str now is %s\n", str);
system("pause");
return 0;
}
本文介绍了一种使用指针实现字符串反转的方法。通过定义两个指针分别指向字符串的起始位置和末尾位置,并利用循环交换这两个位置上的字符来达到反转字符串的目的。最后展示反转后的结果。
93

被折叠的 条评论
为什么被折叠?



