Solved

string comparisons

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

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

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

18 Experts available now in Live!

Get 1:1 Help Now