Solved

String

Posted on 2000-04-27
5
151 Views
Last Modified: 2010-04-02
I have a program which I entered a string in uppercase::

string laura;

cin>>laura;
cout<<laura;

The string I entered is gonna be all upper case. How do I change it to lowercase?
0
Comment
Question by:bea_pr
[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
5 Comments
 
LVL 1

Expert Comment

by:mournblade
ID: 2756680
there is a C fcn called tolower() that changes a character (if it is a letter) from uppercase to lowercase. to convert a string, you would have to write a fcn that takes every letter in the string, apply tolower() on them and store the result back in the string. a simple 'for' loop should do the trick.
0
 
LVL 1

Expert Comment

by:mournblade
ID: 2756687
forgot a detail: to use tolower(), you have to include ctype.h
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2756886
Next is code:
.....
 string laura;
 cin >> laura;
 strlwr ( (char *)laura.c_str());
 cout << laura;
....
I hope, it helps. Alex
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 50 total points
ID: 2758458
> strlwr ( (char *)laura.c_str());

this expression is a -bit- dangerous because you are directly manipulating the strings internal buffer,  which may cause problems (hence the need for a nasty C style cast or a const_cast).

a more correct way to do the same thing would be to do something like:

std::transform(s.begin(), s.end(), s.begin(), ::tolower);

 
0
 
LVL 22

Expert Comment

by:nietod
ID: 2759334
I strongly agree with Jason on that one.  The code

strlwr(laura.c_str());

will not compile because it is unsafe.  The "(char *)" is needed to force the compiler to accept the unsafe code.  That doesn't make it safe, but it makes the compiler ignore the fact that it is unsafe.
0

Featured Post

Industry Leaders: 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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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