計算機等級考試三級網絡技術上機題及答案

2016下半年計算機等級考試備考中,爲方便考生複習好計算機三級網絡技術,yjbys小編特整理最新網絡技術模擬試題及答案解析如下:

計算機等級考試三級網絡技術上機題及答案

1.編寫一個函數findStr( ),該函數統計一個長度爲2的字符串在另一個字符串中出現的次數。例如,假定輸入的字符串爲"asd asasdfg asd as zx67 asd mklo",子字符串爲"as",函數返回值是6。

函數ReadWrite( )的功能是實現從文件中讀取兩個字符串,並調用函數findStr(),最後把結果輸出到文件中。

注意:部分源程序已給出。

請勿改動主函數main() 和其他函數中的任何內容,僅在函數 findStr()的花括號中填入你所編寫的若干語句。

試題程序:

#include

#include

#include

void ReadWrite();

int findStr(char *str,char *substr)

{

}

void main()

{

char str[81],substr[3];

int n;

system("CLS");

printf("輸入原字符串:");

gets(str);

printf("輸入子字符串:");

gets(substr);

puts(str);

puts(substr);

n=findStr(str,substr);

printf("n=%dn",n);

ReadWrite();

}

void ReadWrite()

{

char ch,str[81],substr[3];

int n,len,i=0;

FILE *rf,*wf;

rf=fopen("","r");

wf=fopen("","w");

while(i<>

{

fgets(str,80,rf);

fgets(substr,10,rf);

len=strlen(substr)-1;

ch=substr[len];

if(ch=='n'||ch==0x1a)

substr[len]=0;

n=findStr(str,substr);

fprintf(wf,"%dn",n);

i++;

}

fclose(rf);

fclose(wf);

}【答案】

int findStr(char *str,char *substr)

{

int n=0; /*定義計數器變量,統計出現次數*/

char *p,*r; /*定義指針變量來分別指向兩個字符串*/

while(*str) /*如果字符串沒有結束,則一直循環下去*/

{

p=str; /*指針p指向字符串首地址*/

r=substr; /*指針r指向子字符串首地址*/

while(*r) /*若子字符串沒有結束,則循環繼續*/

if(*r==*p)

/*如果子字符串的第一個字符等於字符串中的該字符,則繼續比較下一個字符*/

{

r++;

p++;

}

else

break; /*否則退出循環*/

if(*r=='