![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
2009年春江蘇計(jì)算機(jī)上機(jī)考試1 |
2009春上機(jī)試卷1 一:改錯(cuò)題: 【題目】以下程序的功能是:查找某個(gè)指定單詞在字符串中出現(xiàn)的次數(shù),單詞之間由一個(gè)或多個(gè)空格分隔,判斷字符時(shí)不區(qū)分字母的大小寫: 例如:在字符串”Abc defabc abc abcge ABC”中,單詞abc出現(xiàn)了3次(下劃線部分) 含有錯(cuò)誤的源程序如下: #include char convert(char c) //本函數(shù)實(shí)現(xiàn)將大寫字母轉(zhuǎn)換為小寫字母 {if(c>='A' && c<='Z') return c+'a'-'A'; //因?yàn)槭寝D(zhuǎn)化為小寫,所以應(yīng)該為c+'a'-'A' return c; } int search(char *str, char *s) {int count=0,flag=0; for(;*str;str++){ if(flag==0 || *str==' '){ flag=1; while(*(str++)==' '); //跳過(guò)空格,使其指向有字母的單詞 str--; for(char *str1=str,*s1=s;*s1;s1++,str1++){ if(convert(*str1)!=convert(*s1)) break; //for循環(huán)判斷s字符串是否在str中 } if(!*s1&&(*(str1)==' '||!*str1)) //如果s1字符串比較結(jié)束了并且str1遇到了空格或 count++; //者是str1在最后就加1 } } return count; } void main() {char str[40]="Abc defabc abc abcge ABC",s[10]="abc"; cout<<"字符串"<<"中單詞"< }
|