有很多人提到字串怎麼比較
因為字串(string)在記憶體中 是已很多個字元(char)來儲存
ex: CAT 儲存為 C A T \0
== 表字串結束
所以說是不能用 str1==str2 來作比較的
僅僅能做的 只能比較 str1[0]==str2[0]
[1]== [2]
.
.
.
故我都會叫人家用 string.h 中的 strcmp()
又以下是我隻前寫過不用strcmp()的祕馬檢查程式
/* 寫一請使用者輸入 password 的程式,輸錯三次及結束執行. */
#include <stdio.h>
int main(void)
{
char pw[80];
char end[]="程式結束\n";
int times=0;
do
{
printf("請輸入您的密碼:\n");
scanf("%s",pw);
if(pw[0]=='1' && pw[1]=='2' && pw[2]=='3' && pw[3]=='4' && pw[4]=='\0')
{
printf("密碼正確!\n");
times+=4;
}
else
{
printf("密碼錯誤!\n");
times++;
}
}
while(times<3);
puts(end);
return 0;
}
===
如有錯誤請指正
===
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.112.240.76