I am trying to compare whether the last letter of the word "A", and the next letter of the word "B" is the same.
I'm iterating over "B"; and comparing within an if statement:
string A = ""
string B = "XYZTTTTLMN"
for (long i = 0; i < B.length(); i ) {
if ( A.back() != B.substr(i,1) ) { ... }
}
I receive an error that says I can't compare a string to a character. But, as far as I can see, A.back() returns a character and B.substr() returns a single digit string, which should be OK?
Any ideas on what I can do?
Much appreciated, thank you!
CodePudding user response:
In C , a string having size=1 is not same as a character. This is the reason you are getting error as string and character cannot be compared as you did.
To solve this you can use below code:
for (long i = 0; i < B.length(); i ) {
if ( A.back() != B.at(i) ) { ... }
}
OR
for (long i = 0; i < B.length(); i ) {
if ( A.back() != B[i] ) { ... }
}