Solved

string comparisons

Posted on 2004-10-04
4
210 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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