我在比較C++中的兩個字符串時出現了一個奇怪的問題。比較C++中的兩個字符串
Ç指向「夢幻」,這是我從printf語句證實。現在我想將它與「夢幻般」的字符串進行比較,但每次都會出現在其他部分並顯示不匹配。既str1和STR2的
COUT聲明還打印相同的輸出「夢幻」,但STR1的長度顯示7和str2的長度爲6
誰能告訴我是什麼問題以及如何解決它。
謝謝。
char *c;
c = strtok (temp,",");
// Here printf ("%s\n",c); prints dreamy
while (c != NULL)
{
std::string Str1 = std::string(c);
std::string Str2 = std::string("dreamy");
cout << "Str1 " << Str1 << "Len" << Str1.length() <<endl; // Len = 7 showing for c = "dreamy"
cout << "Str2 " << Str2 << "Len" << Str2.length() <<endl; // Len = 6 for "dreamy"
if(Str1.compare(Str2) == 0)
{
cout << "Finally Match";
presence[1] = 1;
}
else
cout << " Dont Match";
printf ("%s\n",c);
什麼是'temp'?並請顯示您的'cout <<'語句的確切輸出。 – Angew
NPE答案幫助了我。 「夢幻」後有一個空間strtok – piku