我想編寫一個程序來檢查哪個字符串具有更大的長度而不使用字符串函數。我試圖使用gets()
作爲輸入,但我的程序不斷崩潰。請幫忙!謝謝!無法使用獲取字符串中的gets()C
這裏是我的代碼:在第二while
環
#include <stdio.h>
int l1,l2;
char *st1,*st2;
void accept()
{
gets(st1);
gets(st2);
}
void length()
{
int i = 0;
while (st1[i] != '\0')
{
l1++; i++;
}
i = 0;
while (st2[i] != '\0')
{
l1++; i++;
}
}
int main()
{
accept();
length();
if (l1 > l2)
printf("String #1 is greater in length.\n");
else
printf("String #2 is greater in length.\n");
}
st1和st2不指向任何東西。 – wildplasser
此代碼是否沒有警告地編譯?它運行沒有產生錯誤? –
下面是我最喜歡的所有手冊頁的報價:「**切勿使用gets()'。**」因爲不可能安全使用。 –