C语言:用TC编程:将一个字符串中的所有字符按相反的次序重新放置

2025-03-13 11:32:19
推荐回答(2个)
回答1:

#include
#include
#define N 100

void convert(char s[N])
{
int i,j;
char temp;
for(i=0; i {
j=strlen(s)-1;
temp = s[i];
s[i] = s[j-i];
s[j-i] = temp;
}
printf("Now string:\n%s\n",s);
}

int main()
{
int i;
char str[N];
printf("Enter the string:\n");
gets(str);
printf("Your input string is:\n%s\n",str);
convert(str);
return 0;
}
我简单写了一个,你看是否合适。
仅供参考哈!

回答2:

#include "stdio.h"

void Reverse(char *s)
{
char *p=s;
while(*p != '\0') p++;
p--;
char *q=s;
while(p>q)
{
char temp=*p;
*p=*q;
*q=temp;
q++;
p--;
}
}

main()
{
char a[6]="abcde";
Reverse(a);
printf("%s\n",a);
}