Cstring problem

Nov 1, 2013 at 4:18am
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..
Nov 1, 2013 at 5:17am
You can use the comparison operators '>', '<' or '=='.

What exactly is it that you want to do after comparing?
Nov 1, 2013 at 5:26am
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.
Nov 1, 2013 at 6:36am
Nov 1, 2013 at 6:38am
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 Nov 1, 2013 at 6:40am
Nov 1, 2013 at 6:42am
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 Nov 1, 2013 at 6:45am
Topic archived. No new replies allowed.