问题 问答题

请编一个函数fun(char*str),该函数的功能是把字符串中的内容逆置。
例如,字符串中原有的字符串为asdfg,则调用该函数后,串中的内容为gfdsa。
请勿改动主函数rllain和其他函数中的任何内容,仅在函数proc的花括号中填入所编写的若干语句。
试题程序:
#include<string.h>
#include<conio.h>
#include<stdio.h>
#define N 100
void fun(char*str)

char a[N];
FILE*out;
printf("Enter a string:");
gets(a);
printf("The original string is:");
puts(a);
fun(a);


main()
printf("The string after modified:");
puts(a);
strcpy(a,"Welcome!");
fun(a);
out=fopen("outfile.dat","w");
fprintf(out,"%s",a);
fclose(out);

答案

参考答案:char ch;
int i,m,n;
i=0;
m=n=strlen(str)-1; //求字符串str长度
while(i<(n+1)/2) //循环逆置交换
ch=str[i];
str[i]=str[m];
str[m]=ch;
i++;
m--;
}

解析: 在fun函数中,首先求行字符串的长度,然后通过循环进行字符交换。要注意的是.如果字符串长度是奇数,则最中间的元素在逆置前后的位置是没有改变的。

单项选择题
判断题