Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String

Posted on 2000-04-27
5
Medium Priority
?
155 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 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);

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

610 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