Solved

String

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now