Solved

String

Posted on 2000-04-27
5
141 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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

15 Experts available now in Live!

Get 1:1 Help Now