練習7 10 查詢指定字元 15分

2021-10-10 17:44:31 字數 1649 閱讀 7553

練習7-10 查詢指定字元 (15分)

本題要求編寫程式,從給定字串中查詢某指定的字元。

輸入格式:

輸入的第一行是乙個待查詢的字元。第二行是乙個以回車結束的非空字串(不超過80個字元)。

輸出格式:

如果找到,在一行內按照格式「index = 下標」輸出該字元在字串中所對應的最大下標(下標從0開始);否則輸出"not found"。

輸入樣例1:

m

programming

輸出樣例1:

index = 7

輸入樣例2:

a

1234

輸出樣例2:

not found

錯誤解答:

#include

intsearch

(char c,

char str,

int*pindex)

//search c in str, if found return 1 and give the index, not found return 0

i++;}

return ret;

}int

main()

;//這裡錯了。80個字元成為字串的話需要81大小的字元陣列

int index =0;

fgets

(str,80,

stdin);

//這裡也錯了。這裡fgets最多只能讀79個字元 然後在第80個位置放上'\0'if(

search

(c, str,

&index)

)printf

("index = %d"

, index)

;else

printf

("not found");

return0;

}

正確解答:

#include

intsearch

(char c,

char str,

int*pindex)

//search c in str,

//if found return 1 and give the index, not found return 0

i++;}

return ret;

}int

main()

;int index =0;

fgets

(str,81,

stdin);

if(search

(c, str,

&index)

)printf

("index = %d"

, index)

;else

printf

("not found");

return0;

}

練習7 10 查詢指定字元 15分

進博主技術群,與大佬交流,領取乾貨學習資料 快速找到所需題目 浙大版c語言程式設計第三版題目集一覽表 本題要求編寫程式,從給定字串中查詢某指定的字元。輸入格式 輸入的第一行是乙個待查詢的字元。第二行是乙個以回車結束的非空字串 不超過80個字元 輸出格式 如果找到,在一行內按照格式 index 下標 ...

練習7 10 查詢指定字元 15分

本題要求編寫程式,從給定字串中查詢某指定的字元。輸入的第一行是乙個待查詢的字元。第二行是乙個以回車結束的非空字串 不超過80個字元 如果找到,在一行內按照格式 index 下標 輸出該字元在字串中所對應的最大下標 下標從0開始 否則輸出 not found mprogramming index 7 ...

練習7 10 查詢指定字元 15分

本題要求編寫程式,從給定字串中查詢某指定的字元。輸入格式 輸入的第一行是乙個待查詢的字元。第二行是乙個以回車結束的非空字串 不超過80個字元 輸出格式 如果找到,在一行內按照格式 index 下標 輸出該字元在字串中所對應的最大下標 下標從0開始 否則輸出 not found 輸入樣例1 mprog...