72、下列程序的功能是:把S字符串中的所有字符左移一个位置,串中的第一个字符移到最
后.请编写函数CHG(CHAR *S)实现程序要求,最后调用函数READWRITEDAT()把结果输出到
OUT.DAT文件中.
例如:S字符串中原有内容为:MN.123XYZ,则调用函数后,结果为:N.123XYZM.
# include"string.h"
# include"stdio.h"
# include"ctype.h"
# define N 81

void readwritedat();

void chg(char *s)
{ int i;
 char ch=s[0];
 for (i=0;i<=strlen(s)-1;i++)
  s[i]=s[i+1];
    s[i]=ch;
 

}

main()
{
char a[N];
printf("enter a string :");
gets(a);
printf("the original string is :");
puts(a);
chg(a);
printf("the string after modified:");
puts(a);
readwritedat();
}

void readwritedat()
{
int i;
char a[N];
FILE *rf,*wf;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for(i=0;i<10;i++)
{fscanf(rf,"%s",a);
chg(a);
fprintf(wf,"%s\n",a);
}
fclose(rf);
fclose(wf);
}

/* 注:strcpy(s,s+1)的作用是将第二个字符及以后的字符前移一位,注意这时最后一
个字符还不是原来的第一个字符,用*(s+i-1)=c;来将原第一个字符存入串尾,最后还
要赋一个新的串结束符。*/
void chg(char *s)
{
int i=strlen(s);
char c=*s;
strcpy(s,s+1);
*(s+i-1)=c;
*(s+i)='\0';
}

返回南开百题目录

www.163164.cn 联系QQ:3149886