Cstring problem

How can I check characters in a single string (i.e., how can I compare)?
strcmp() compares two strings; but I need say for an example:
"orange"
I want to compare the characters inside "orange"- 'o' with 'e', 'r' with 'g' and so on.
Plz help me..
You can use the comparison operators '>', '<' or '=='.

What exactly is it that you want to do after comparing?
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string str1 = "orange";
char* cstr1 = "orange";
std::string str2 = "apples";
char* cstr2 = "apples";

if(str1[0] == str2[0]) //use subscript operator to get the nth character
{
     ....
} 
if(cstr1[0] == cstr2[0]) //use subscript operator to get the nth character
{
     ....
} 

For more info. you can refer to the tutorials on this site.
I have to cin>> user input.
A string like, 11101001.
Inside this string I want to compare 1st bit with last bit. If matches, I say YES.
How?
Last edited on
Follow a k n's advice.

1
2
3
4
5
6
char *myBinary;
cin >> myBinary;

if ( myBinary[0] == myBinary[ strlen( myBinary ) - 1 ] ) {
  std::cout << "YES";
}


Caveat: I'm not much conversant with cstrings so above code needs to be verified.
Last edited on
Topic archived. No new replies allowed.