赞
踩
方法1:
#include<stdio.h>
#include<string.h>
void reverse(char *s){
int n = 0;
int m = strlen(s) - 1;
while(n<m){
char tmp = s[n];
s[n] = s[m];
s[m] = tmp;
n++;
m--;
}
}
int main(){
char s[] = "Hello world";
reverse(s);
printf("%s\n",s);
return 0;
}
方法2:
#include<stdio.h>
#include<string.h>
void reverse(char *s){
int i;
for(i=0;i<strlen(s)/2;i++){//循环次数不超过字符串长度的二分之一,否则逆序完成再逆序等于没有变化
char tmp = s[i];
s[i] = s[strlen(s) - i - 1];
s[strlen(s) - i - 1] = tmp;
}
}
int main(){
char s[] = "Hello world";
reverse(s);
printf("%s\n",s);
return 0;
}
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。