Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was reading some articles to understand how can I compare two strings. The examples that they have mentioned were a bit tough for me to understand. I wanted to if anyone can explain me in simple terms with examples.

Thank you.

What I have tried:

ifstream pgmFile;
pgmFile.open(fileName);
string temp;
string comp = "P5";


for(int i = 0; i < 2; i++)
{

    pgmFile >> noskipws >> temp;
    cout << temp;

}

if(temp == comp)
{}
else
    cout << "File does not contain 'P5'.  Please provide the correct type of file." << endl;
Posted
Updated 28-Sep-21 12:38pm

That code doesn't do what it says: the equality test for two strings does not do a "contains" operation, it compares the content and returns true if they are identical, and false when it encounters the first single character difference between them.

The file could well contain "P5" but not on a line by itself ...

To understand string comparison, start here:
relational operators (string) - C++ Reference[^]
 
Share this answer
 
If the file name only has to include "P5" anywhere, you would write
C++
if(temp.find(comp) != string::npos)...
But you mentioned a file type, so maybe you want the file name to end in ".P5". If so, define comp as ".P5" and write
C++
if(temp.rfind(comp) == (temp.size() - comp.size()))...
That is, if temp is "foo.P5", its size (length) is 6 characters, and the size of comp is 3, so the match must occur at position 3, which is the '.' when counting up from 0.

However, wouldn't "p5" also be valid? Whether to treat upper and lower case the same is often a consideration when dealing with strings.

And if the user actually provides temp, why not just append ".p5" to it rather than force them to type it in if it's the only acceptable file type?

EDIT: Changed .find to .rfind in the second code fragment to allow ".p5" to occur earlier in the file name.
 
Share this answer
 
v2
Comparing two strings is depending what type of strings you have. When you have some class objects than you normally have some operators or functions to compare them. Like CString from the MFC
C++
CString a("PS"), b("PS");
a.CompareNoCase(b);
The other case is that you compare the strings as character arrays element after element which does strcmp.
Sometimes it is a mix of it, when you have one class objects and a character arrays. That can provide some pitfalls when they have different character encoding. The rule is to first encode in the same scheme, normally the more complex, and than compare.
 
Share this answer
 
To compare strings in C ++, it is not a good idea to compare the two objects directly with ==. With that you would perhaps find out whether it is the same object. The compare method is usually used to compare the text in two different string instances.

Explanation e.g. here:
https://www.cplusplus.com/reference/string/string/compare/

The text "Please provide the correct type of file" irritates me.

When it comes to finding a file type, a simple comparison is not enough.
See also the proposed solutions by Greg Utas and OriginalGriff.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900