LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2003, 01:51 PM   #1
danxl
LQ Newbie
 
Registered: Jul 2003
Posts: 11

Rep: Reputation: 0
compare string in C++


How to do case sensitive/insensitive string comparation with std::string?

and, how to convert string upper case or lower case?
 
Old 11-02-2003, 02:04 PM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
Code:
int strcmp_nocase (const string S1, const string S2)
{
	string::const_iterator p1 = S1.begin ();
	string::const_iterator p2 = S2.begin ();

	while (p1 != S1.end () && p2  != S2.end ())
	{
		if (toupper (*p1) != toupper (*p2))
		{
			if (toupper (*p1) < toupper (*p2))
			{
				return -1;
			}
			else
			{
				return 1;
			}
		}
		++p1;
		++p2;
	}
	if (S1.size () == S2.size ())
	{
		return 0;
	}
	else if (S1.size () < S2.size ())
	{
		return -1;
	}
	else
	{
		return 1;
	}
}
you should be able to tweek the code to do case sensitive comparisons. you can also see there are ways to toupper (tolower would be the same) a string. not too hard.

Last edited by megaspaz; 11-02-2003 at 02:10 PM.
 
Old 11-02-2003, 02:10 PM   #3
danxl
LQ Newbie
 
Registered: Jul 2003
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks to you reply.
Just wonder why codes like this is not included in the std C++ library.
 
Old 11-02-2003, 02:14 PM   #4
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
if you grabbed the to_lcase, to_ucase code, i wouldn't use it. there's a memory leak in both of those. that's why i took it down.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
compare string in SQL hus Programming 9 04-27-2005 10:17 AM
how to compare decimals linuxboy69 Linux - Software 2 08-16-2004 02:43 PM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration