简体   繁体   English

比较c ++中的两个字符串

[英]Comparing two strings in c++

I am getting a weird problem in comparing two string in c++. 我在比较c ++中的两个字符串时遇到了一个奇怪的问题。

c pointing to "dreamy" , which I have confirmed from the printf statement. c指向“梦幻”,我已从printf声明中确认。 Now I want to compare this with "dreamy" string but every time it is going in else part and showing not matching. 现在我想将它与“梦幻”字符串进行比较,但每次它都在其他部分并显示不匹配。

cout statement of both Str1 and Str2 also printing same output "dreamy" but the length of Str1 is showing 7 and length of Str2 is 6. Str1和Str2的cout语句也打印相同的输出“dreamy”,但Str1的长度显示为7,Str2的长度为6。

Can anybody tell me what is the problem and how to fix it. 任何人都可以告诉我这是什么问题以及如何解决它。

Thanks. 谢谢。

        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);

Len = 7表示第一个字符串(可能是空格或换行符)中存在虚假字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM