Solved

String

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

810 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