Hello friends
I just started learning C++ i want to know How to Convert UPPERCASE CHARACTERS Into lowercase characters in C++ ?
Can anyone help me
Thanks
Hello friends
I just started learning C++ i want to know How to Convert UPPERCASE CHARACTERS Into lowercase characters in C++ ?
Can anyone help me
Thanks
// Following example, converts UPPERCASE characters in a string to lower case string
# include <iostream.h> // Header file for input output streams
# include <string.h> // Header file for string related functions such as strlen()
// Here is the function declaration "StrToLwr" for converting upper case characters in a string to lower case characters. The function takes two arguments. Both are char type pointers. First one is the actual string passed to it for conversion. The second argument will send the result back to the calling program. The function will convert only UPPERCASE CHARACTERS to lowercase characters. Every other character will remain as it is
void StrToLwr(char* mainstring, char *output)
{
int Len = strlen(mainstring); // Len will contain the length of the passed string.
for(int i = 0; i < Len; i++) // Now we will move from the 0th character in the string till its length and we will check if its a UPPERCASE character and then change it to lowercase characters.
{
if( (int)mainstring[i] < 65) // If the ASCII code of the character i.e. integer value of the character is less than 65, then copy the character as it is into the result string. It means that the character is not in the range of upper case characters
output[i] = mainstring[i];
if( (int)mainstring[i] >= 65 && (int)mainstring[i] <= 90) // If the ASCII code of the character i.e. integer value of the character is between 65 and 90, then its a UPPERCASE CHARACTER and let's convert it into the lowercase. By adding 32 in the ASCII number of the character, the character will be converted to lowercase. Such as, the ASCII code of UPPERCASE A is 65 and the ASCII code of lowercase a is 97. If we add 32 to UPPERCASE A, its ASCII code will become 97 and the character is changed from UPPERCASE to lowercase
output[i] = (char)mainstring[i] + 32;
if( (int)mainstring[i] >= 97 && (int)mainstring[i] <= 122) // If the ASCII code of the character i.e. integer value of the character is between 92 and 122, then copy the character as it is into the result string. It means that the character is not in the range of upper case characters. This is the range for lowercase characters
output[i] = (char)mainstring[i];
if( (int)mainstring[i] > 122) // If the ASCII code of the character i.e. integer value of the character is greater than 122, then copy the character as it is into the result string. It means that the character is not in the range of upper case characters. This range contains the graphical ASCII characters.
output[i] = mainstring[i];
}
output[i] = '\0'; // Null terminating the string to show the end of the string
}
// Here is the main program which will be using the C_File function
void main()
{
char arr1[21]="PRAsHANT"; // String to be converted
char arr2[21]; // Result will be stored here
StrToLwr(arr1,arr2); // Calling the function
cout<<arr2; // Display the result
} // End of main function
Here is the Program that will convert lower case letters to upper case
Code:#include<iostream.h> #include<conio.h> void main() { clrscr(); char ch; cout<<"enter a character"; cin>>ch; { if((ch>=65)&&(ch<=122)) { if((ch>=97)&&(ch<=122)) ch=ch-32; cout<<"The upper case character corresponding is"<<endl;} {if((ch>=65)&&(ch<=90)) ch=ch+32; cout<<"The lower case character corresponding is"<<endl;} cout<<"the character is not a letter"<<endl; } getch(); }
#include<iostream.h>
void main()
{
char a[50];
cin.getline(a,50);
for(int i=0;a[i]!='\0';i++)
{
if(a[i]>=65&&a[i]<=90)
a[i]+=32;
}
cout<<endl<<a;
}
Bookmarks