Solved

string comparisons

Posted on 2004-10-04
4
207 Views
Last Modified: 2010-04-01
Hi if I define two strings s1,s2 why can't I use strcmp(s1,s2) ?

however I can check if (s1==s2) but what about comparing abel and ABel in this case (s1==s2) doesn't work.

Are there any functions that convert a string to lower case or upper case.

I solved this by using strcmp (s1.c_str(),s2.c_str()) but in case of using long arrays of string won't that be an overhead to covert s1 to s1.c_str()

0
Comment
Question by:anshuma
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:Indrawati
Indrawati earned 150 total points
ID: 12223095
Simple answer:

#include <algorithm>
(for_each(a.begin(), a.end(), tolower) == for_each(b.begin(), b.end(), tolower))

will do case-insensitive comparison.

For another way to do it, you may look at GoTW #29:
http://www.gotw.ca/gotw/029.htm
0
 
LVL 3

Expert Comment

by:Indrawati
ID: 12223134
Sorry, the last answer should be:

      string a = "ABEL";
      string b = "abel";

      transform(a.begin(), a.end(), a.begin(), tolower);
      transform(b.begin(), b.end(), b.begin(), tolower);
      cout << a == b;

I confused transform() with for_each()... again.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 50 total points
ID: 12223195
> I solved this by using strcmp (s1.c_str(),s2.c_str()) but in case of using long arrays of string won't that be an overhead
> to covert s1 to s1.c_str()
Actually there is not conversion, just some **arrangements** to give you a pointer to the string buffer, ensuring it is null terminated to be handled correctly by functions like strcmp() or stricmp().

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
ID: 12225059
std::string isn't very comfortable regarding case insensitivity and other regular string functionality. Mostly, it's because std::string is a template class but case sensitivity easily could be handled only for (ASCII) char type.

You may help yourself by defining an own operator== function like that:

bool operator== (const string& s1, const string& s2)
{
    return stricmp(s1.c_str(), s2.c_str()) == 0;
}

The disadvantage is that you've to put the definition to any IDE you or others are working with code depending on that feature.

Regards, Alex


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 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

16 Experts available now in Live!

Get 1:1 Help Now