473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Character Handling Question

Hello,

I've two quick questions. Suppose I've got the following:

char* cptr = "This is potentially, a really, really long string";

I need to determine if the string contains "PULP FICTION" without regard
to case. For example, all of "PULP FICTION", "pulp fiction", and PuLp
Fiction" meet my criteria. I don't care WHERE in the character array it
occurs, I just want to know if it's there. Is there a standard library
function which will do this for me or do I need to write something
myself? Either something which will handle the char* directly or
somethign in std::string; either would be okay. I've poked around a
fair bit and can't find anyting, but it seems this should be a pretty
common kind of thing to do.

On a related note: If I follow the line above with this:

std::string myString = cptr;

does this involve copying the entire contents of cptr, or does the
automatic myString implement copy on write semantics? I suspect the
former but it couldn't hurt to ask. Thanks in advance for any replies!

-exits

Jul 22 '05 #1
1 1413

"Exits Funnel" <ex*********@noyahoospam.com> wrote in message
news:oz*********************@news4.srv.hcvlny.cv.n et...
Hello,

I've two quick questions. Suppose I've got the following:

char* cptr = "This is potentially, a really, really long string";

I need to determine if the string contains "PULP FICTION" without regard
to case. For example, all of "PULP FICTION", "pulp fiction", and PuLp
Fiction" meet my criteria. I don't care WHERE in the character array it
occurs, I just want to know if it's there. Is there a standard library
function which will do this for me or do I need to write something
myself? Either something which will handle the char* directly or
somethign in std::string; either would be okay. I've poked around a
fair bit and can't find anyting, but it seems this should be a pretty
common kind of thing to do.

Its possible to write a case insensitive string class by createing a custom
char_traits class.

class CaseInsensitveCharTraits : public std::char_traits<char>
{
...
};

typedef std::basic_string<char, CaseInsensitveCharTraits>
CaseInsensitiveString;

You can look up the details in The C++ Standard Library by Josuttis. Once
you done that all the regular string handling functions will work in a case
insensitive manner, no need for special functions. So you could just write

CaseInsensitiveString x = "This is potentially, a really, really long
string";
CaseInsensitiveString y = "pulp fiction";
if (x.find(y) != CaseInsensitiveString::npos)
{
...
}

This is definitely worth doing (and learning about) if you plan to do a lot
of case insenstive string handling.
On a related note: If I follow the line above with this:

std::string myString = cptr;

does this involve copying the entire contents of cptr, or does the
automatic myString implement copy on write semantics? I suspect the
former but it couldn't hurt to ask. Thanks in advance for any replies!

The former.

string may implement copy on write in general, but it can't do in the
example you quote because it can't 'take ownership' of a char*. What if the
char* was later deleted? The string would end up pointing at deleted memory.
-exits


john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
21
by: aegis | last post by:
7.4#1 states The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
17
by: Gladiator | last post by:
When I am trying to execute a program from "The C Programming Language" by Dennis Ritchie, I tried to run the following program.I am using Dev++ as a compiler software. The Program is presented...
44
by: Kulgan | last post by:
Hi I am struggling to find definitive information on how IE 5.5, 6 and 7 handle character input (I am happy with the display of text). I have two main questions: 1. Does IE automaticall...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
1
kaleeswaran
by: kaleeswaran | last post by:
Hi! i am handling password generation module..... i need to force the user enter the special character in their password thing.so i need to handle the special character.so...
5
by: qiqinuinaifen128 | last post by:
Hi Pros, I have a question regarding handling chinese character. I have an text area for user to key in sth. But when the user key in Chinese Character and click a button. The problem is when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.