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

string laura;


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

Expert Comment

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.

Expert Comment

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

Expert Comment

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

Accepted Solution

jasonclarke earned 200 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);

LVL 22

Expert Comment

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


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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
Suggested Courses

762 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