第一題:填空題 請補充fun函數(shù)的功能是在字符串的最前端加入n個*號,形成新串,并且覆蓋。
注意:字符串長度最長允許為79。
請勿改動主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在fun函數(shù)的橫線上填入所寫的若干表達式或語句。
#include
#include
#include
void fun(char s[], int n)
{
char a[80], *p;
int i;
p = ___1___;
for (i=0; i
a[i] = '*';
do
{
a[i] = ___2___;
i++;
} while (___3___);
a[i] = 0;
strcpy(s, a);
} 考試大(www.Examda。com)
main()
{
int n;
char s[80];
printf("\nEnter a string:");
gets(s);
printf("\nThe string\"%s\"\n", s);
printf("\nEnter n(number of*):");
scanf("%d", &n);
fun(s, n);
printf("\nThe string after insert:\"%s\"\n", s);
}
參考答案:
填空題:第1處填空:s
第2處填空:*p++
第3處填空:*(p) 或*p 或者 *p!=0
第二題:改錯題 下列給定程序中函數(shù)fun的功能是:先將在字符串中s中的字符按逆順序存放到t串中,然后把s中的字符按正序連接到t串的后面。例如:s中的字符串為ABCDE時,則t中的字符串應(yīng)為EDCBAABCDE.
請改正程序中的錯誤,使它能得出正確的結(jié)果。
注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include
#include
#include
void fun(char *s, char *t)
{
int s1, i;
s1 = strlen(s);
/********found********/
for (i=0; i
t[i] = s[s1-i];
for (i=0; i
t[s1+i] = s[i];
t[2*s1] = '\0';
}
main()
{
char s[100], t[100];
printf("\nPlease enter string s:");
scanf("%s", s);
fun(s, t);
printf("The result is: %s\n", t);
}
參考答案:
改錯題:第1處:t[i]=s[s1-i];應(yīng)改為t[i]=s[s1-i-1];或 t[i]=s[s1-1-i];
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |