2006年9月全國等級考試三級c語言上機題庫(十一)

☆題目11(無憂id 93 字元串字母移位題)

 

程式prog1.c的功能是:把 s 字元串中的所有字母改寫成該字母的下一個字母,字母z改寫成字母a。要求大寫字母仍為大寫字母,小寫字母仍為小寫字母,其它字元不做改變。

請考生編寫函式chg(char *s)實現程式要求,最後調用函式readwritedat( )把結果輸出到檔案bc1.out中。

例如:s 字元串中原有的內容為:mn.123zxy,則調用該函式後,結果為:no.123ayz。

注意:部分源程式存在檔案prog1.c檔案中。

請勿改動主函式main( )和輸出數據函式readwritedat()的內容。

#include <conio.h>

#include <string.h>

#include <stdio.h>

#include <ctype.h>

#define n 81

void readwritedat();

 

void chg(char *s)

{while(*s)

if(*s=='z'||*s=='z') {*s-=25; s++;}

else if(*s>='a'&&*s<='y') {*s+=1;s++;}

else if(*s>='a'&&*s<='y') {*s+=1;s++;}

else s++;

}

 

main( )

{

char a[n];

clrscr();

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("bc1.in", "r") ;

wf = fopen("bc1.out", "w") ;

for(i = 0 ; i < 50 ; i++) {

fscanf(rf, "%s", a) ;

chg(a) ;

fprintf(wf, "%s\n", a) ;

}

fclose(rf) ;

fclose(wf) ;

}