The program converts a string form LOWERCASE to UPPERCASE using a function strupr() found in the C string library(<cstring>).
It has the following syntax : strupr(char *);
This means that it takes the address of the first character of the target string and converts it into uppercase.
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 strupr().
*Note : strlwr() and strupr() are not standard C++ functions. Functions such as tolower() and toupper() found in ctype.h are standard c++ functions.
*Note : strlwr() and strupr() are not standard C++ functions. Functions such as tolower() and toupper() found in ctype.h are standard c++ functions.
#include <iostream>
#include <cstring> // header file that contains the strupr() function
int main()
{
char string1[20],*ptr;
std::cout<<" Enter a string of your choice(max 20 characters : ";
std::cin.getline(string1,sizeof(string1)); // stores the string in the string1 variable
ptr = &string1[0]; // assigns the address of the first element to the pointer variable of char type
strupr(ptr); // converts the string to uppercase
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 lowercase 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 strupr() 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 strupr() function causes changes in the original string, hence it should not be used if the original string is to be preserved.
*Note : strlwr() and strupr() are not standard C++ functions. Functions such as tolower() and toupper() found in ctype.h are standard c++ functions. The following program demonstrates the use of toupper() function to convert strings from lowercase to uppercase.
// Program to convert lowercase and uppercase (using standard C++ functions)
#include <iostream>
#include <cctype>
int main()
{
char string1[20],string2[20];
std::cout<<" Enter a string in lowercase (max 20) : ";
std::cin.getline(string1,sizeof(string1)); // gets the input string
int i;
// keep running the loop until \0 is not occuered i.e end of string
for (i=0; string1[i] != '\0'; i++)
{
string2[i] = (char)toupper(string1[i]);
}
string2[i] = '\0'; // append \0 at after the last character
std::cout<<" String in UPPERCASE is : "<<string2; // displays the output string
std::cin.get();
return 0;
} // end of main
*Note : strlwr() and strupr() are not standard C++ functions. Functions such as tolower() and toupper() found in ctype.h are standard c++ functions. The following program demonstrates the use of toupper() function to convert strings from lowercase to uppercase.
// Program to convert lowercase and uppercase (using standard C++ functions)
#include <iostream>
#include <cctype>
int main()
{
char string1[20],string2[20];
std::cout<<" Enter a string in lowercase (max 20) : ";
std::cin.getline(string1,sizeof(string1)); // gets the input string
int i;
// keep running the loop until \0 is not occuered i.e end of string
for (i=0; string1[i] != '\0'; i++)
{
string2[i] = (char)toupper(string1[i]);
}
string2[i] = '\0'; // append \0 at after the last character
std::cout<<" String in UPPERCASE is : "<<string2; // displays the output string
std::cin.get();
return 0;
} // end of main
lowercase to uppercase (alternate version) |
0 comments:
Post a Comment
Comment Here....