Copyright © Programs ++
Design by Dzignine
Thursday 26 January 2012

Uppercase to Lowercase

The program converts a string form UPPERCASE to LOWERCASE using a function strlwr() found in the C string library(<cstring>).
It has the following syntax :  strlwr(char *);
This means that it takes the address of the first character of the target string and converts it into lowercase. 
For example, if the input string was : HELLO, MY NAME IS DEEPAK.
The output would be : hello, my name is deepak. 
A sample program is written to demonstrate the use of strlwr().

// Program to convert a string from uppercase to lowercase 

#include <iostream>
#include <cstring> // header file that cotains the function strlwr

int main()
{
    char string1[20],*ptr;
    std::cout<<" Enter a string of your choice(max 20 characters : ";
    std::cin.getline(string1,sizeof(string1));
    ptr = &string1[0]; // assigns the address of the first character of the sting to ptr
    strlwr(ptr); // convers the string to lowercase
    std::cout<<" \n The string is as follows : "<<string1;
    std::cin.get(); // displays the output
    return 0;
} // end of main 

 

sizeof() --  the sizeof() calculate the total size of a variable,string or an object. 
The above program prompts the user to enter a string in uppercase and stores it in string1 variable.
Then it assigns the address of the first element of the sting to the ptr pointer which is of char type, because in the strlwr() function the whole string is not passed, instead the address of the first element of the string is passed. 
However, one should keep in mind that using strlwr() function causes changes in the original string, hence it should not be used if the original string is to be preserved.

Note : strupr() and strlwr() are not standard C++ functions, standard c++ functions for converting uppercase to lowercase and vice-verse, use tolower() and toupper() functions located in <cctype> or for those who use turbo c++ , it is located in the ctype.h header file. Click the link below to gain for a demo program:
http://programsplusplus.blogspot.com/2012/01/lowercase-to-uppercase.html


0 comments:

Post a Comment

Comment Here....