開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
lubuntu + gcc 4.9.2
問題(Question):
我比較"abcd"跟"efgh"結果是"They are different!"
可是若是我打"abcd"跟"abcd " (後者多一空格) 或"ab cd"跟"abcd"
結果是"They are the same!"
請問該如何解決他不認得空格的問題?
謝謝
餵入的資料(Input):
ab cd 與 abcd
預期的正確結果(Expected Output):
"They are different!"
錯誤結果(Wrong Output):
"They are the same!"
程式碼(Code):(請善用置底文網頁, 記得排版)
全部程式碼在此:
https://gist.github.com/gnitnaw/127d36d1eca96f431ef9
或看以下程式碼(跟上面的link一樣):
#include <stdio.h>
#define MAXSIZE 80
void strcomp(char* p1, char* p2);
void read_string(char *pt);
int main(void)
{
char line1[MAXSIZE], line2[MAXSIZE];
printf("Enter first string : ");
read_string(line1);
printf("%s\n", line1);
printf("Enter second string : ");
read_string(line2);
printf("%s\n", line2);
strcomp(line1, line2);
return 0;
}
void read_string(char *pt)
{
int i;
for(i=0; i < MAXSIZE; i++)
{
if (getchar() != '\n')
{
pt[i] = getchar();
} else {
break;
}
}
}
void strcomp(char* p1, char* p2)
{
while (*p1 == *p2) {
if (*p1 == '\0' || *p2 == '\0') {
break;
}
++p1;
++p2;
}
if (*p1 == '\0' && *p2 == '\0') {
printf("They are the same! \n");
} else {
printf("They are different! \n");
}
}