简答题 C语言
题号 #2257536

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


}
void main()
{
char a[N];
FILE*out:
printf("Enter a string:");
gets(a);
printf("The origir al string is:");
puts(a);
fun(a):
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()函数中,首先求行字符串的长度,然后通过循环进行字符交换。要注意的是,如果字符串长度是奇数,则最中间的元素在逆置前后的位置是没有改变的。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考