2013-04-12 131 views
1

我在比较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); 
+0

什么是'temp'?并请显示您的'cout <<'语句的确切输出。 – Angew

+0

NPE答案帮助了我。 “梦幻”后有一个空间strtok – piku

回答

2

Len = 7表明有第一个字符串中的伪字符(可能是空格或换行)。

+0

谢谢... 是的,有梦幻般的空间... – piku

+5

这就是为什么我喜欢打印字符串使用'cout <<'['<< str <<' ]'':) – Turgal