Solved

String Compare in C

Posted on 2011-03-07
7
666 Views
Last Modified: 2012-05-11
I'm doing a quick string compare in C and I keep getting the incorrect results.

I'm guessing that the way that a string is stored differently than it's actual displayed value, or there's otherwise some cranny behind the scenes of the coding that is causing this to occur.

Anyway, if I run code like the following, I get the display of

02
Hello

Which doesn't make any sense, any ideas?
string Letter = "02";

		cout << Letter << "\n";
		
		if (Letter.compare("01") == 1) {
			cout << "Hello" << "\n";
		}

Open in new window

0
Comment
Question by:PGRBryant
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
xchange earned 334 total points
Comment Utility
You assume that upon the strings being equal the returned value is 1 which is in fact not.
Compare returns zero upon string equality.
0
 
LVL 2

Assisted Solution

by:xchange
xchange earned 334 total points
Comment Utility
"02" (the object) is "GREATER" than "01" the parameter and according to the function's spec:
+1 : a positive is returned.

Let me know if you need any additional clarification.
0
 
LVL 1

Author Comment

by:PGRBryant
Comment Utility
Ahhh,

My actual problem was a little more complicated, I had a series of If statements that followed that question which asked

Is it "01" is it "02" is it "03"...is it "15"... else... with each one redefining Letter.

And I kept getting an "else" result, but now I realized that it was because the redefinition was just causing them all to display false.. thanks.

Is 0 always true and 1 always false, can you redefine that, how does that work in C?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:xchange
Comment Utility
>Is 0 always true and 1 always false, can you redefine that, how does that work in C?
If you mean to change the function's behaviour you can do that through overloading I suppose - the details evade my memory - but do you REALLY want to do this?
(Just because you can does not mean you should - right?)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 166 total points
Comment Utility
Your zone and question indicate the C language, but your code post indicates the C++ language. Why are you interested in C suggestions when writing in C++?

Since you are writing in C and using the string class (which is stored differently than your c-style string, as you already guessed), you are naturally using the string compare function.

To see how to use the compare , here is code from
     http://www.cplusplus.com/reference/string/string/compare/

Notice that there are a number of different flavors of compare giving you quite a bit of flexibility.
// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(str2) != 0)
    cout << str1 << " is not " << str2 << "\n";

  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    cout << "therefore, both are apples\n";

  return 0;
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:PGRBryant
Comment Utility
I'm trying to learn C but the best compiler option that I could find was C++ 2010 Express, so I figured I'd just get a little acquainted with both along the way.

And xchange and you both answered my question, so points awarded and much thanks.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Well good luck in your studies! I wish you the best. I think VS C++ 2010 Express is one of the best IDE's around for learning purposes.

It would be helpful for us and for searching the PAQ (Previously Answered Questions) by separating your C and C++ question into two separate questions, one in the C zone, and one in the C++ zone. For the C zone, be sure to post only C code so as not to confuse anyone. For the C++ zone, if you indicate that you are not looking for C-like answers, then you will get responses that hopefully will address the best C++ solutions. (I have seen pure C solutions being given in C++ zones; and I think I may have been guilty of that myself.)

By asking a C zone question and posting C++ code is very confusing to me. It also is like asking two different questions in one question. Please take a look at this:
     http://www.experts-exchange.com/help.jsp?hi=23

Again, I wish you the best in your studies.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
mergeTwo  challenge 13 70
changePi Challenge 15 75
parentbit challenge 3 50
This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now