自定義函式bool fun(char *s, int start , int len, char *&subs),函式的功能:由已知串s得到它的一個子串subs,子串從s的start(下標)開始,長度為len,取子串操作成功,函式回傳true,否則回傳false。并設計相應的測驗程式。(不得使用字串操作庫函式)
求解~
uj5u.com熱心網友回復:
一個一個字符拷貝給subsuj5u.com熱心網友回復:
bool fun(char *s, int start, int len, char *&subs){
if (s == NULL)
return false;
char* str = s;
int nCount = 0;
while (*str++ != NULL)
++nCount;
if (start + len > nCount)
return false;
if (subs == NULL)
{
subs = s + start;
}
else
{
str = s + start;
for (int i = 0; i < len; ++i)
{
subs[i] = str[i];
}
subs[len] = 0;
}
return true;
}
int main()
{
char sub[100];
char* psub = sub;
fun("this is test string!", 3, 5, psub);
system("pause");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/24065.html
標籤:C++ 語言
下一篇:可能是庫函式出了問題,求解答
