Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i want to have code to convert uppercase to lowercase in c++.Please provide the requested function.
Posted

Try this:

#include <ctype.h>
void stringtolower(char *input)
{
    for(int i=0;input[i];i++)
        input[i] = tolower(input[i]);
}
 
Share this answer
 
v3
Hi,
Thaddeus's answer using ::tolower() is OK for plain ANSI char based strings.

The following works as well with non-ANSI character sets or wchar_t based strings:
C++
// Use with VC pre-2010 compilers
#include <string>
#include <locale>
#include <algorithm>
#include <iterator>

template <class C_type>
C_type CharLower(C_type in)
{
    return std::tolower(in, std::locale());
}
template <typename S_type>
S_type ToLower(const S_type& str)
{
    S_type out;
    std::transform(str.begin(), str.end(), std::back_inserter(out), CharLower<S_type::value_type>);
    return out;
}

or simpler for VC2010 because of C++0x partial implementation:
C++
// Use with VC2010 compilers
#include <string>
#include <locale>
#include <algorithm>
#include <iterator>

template <typename S_type>
S_type ToLower(const S_type& in)
{
    S_type out;
    std::transform(in.begin(), in.end(), std::back_inserter(out), [](S_type::value_type ch){
        return std::tolower(ch, std::locale());});
    return out;
}
You can test both versions with:
C++
#include <iostream>
int main ()
{
    using namespace std;
    string str="Test String.\n";
    cout << ToLower(str) << endl;

    wstring wstr= L"Test String.\n";
    wcout << ToLower(wstr) << endl;

    return 0;
}


cheers,
AR
 
Share this answer
 
Comments
Rajesh R Subramanian 8-Dec-10 11:11am    
Man, you've way too much of time in your hands. :)
Alain Rist 8-Dec-10 11:36am    
These kind of questions help me convert old snippets to C++0x, with the pleasure of code improvement :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900