开发平台(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");
}
}