Solved

string comparisons

Posted on 2004-10-04
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

710 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