【问题描述】
编写一函数,利用指针实现把字符串内容逆置。
要求:
1、不在子函数中输出。
2、不能修改指定的子函数:
void StrReverse(char *st)
【输入形式】
输入字符串。
【输出形式】
输出逆置后的字符串
【输入样例】
CCZU
【输出样例】
UZCC
方法一
#include<stdio.h>
#include<string.h>
void StrReverse(char *st)
{
char *p=st,c;
while(*p)
p++;
p--;
while(st<p)
{
c=*st;
*st++=*p;
*p--=c;
}
}
int main()
{
char a[50];
scanf("%s",a);
StrReverse(a);
printf("%s",a);
}
方法二
#include<stdio.h>
#include<string.h>
void StrReverse(char *st)
{
int i=0,n;
char t;
n=strlen(st)-1;
for(i=0;i<n;i++,n--)
{
t=st[i];
st[i]=st[n];
st[n]=t;
}
}
int main()
{
char st[100];
int i=0;
scanf("%s",st);
StrReverse(st);
while(st[i]!='\0')
{
printf("%c",st[i]);
i++;
}
return 0;
}