Welcome to the exciting world of C++ programming! Today, we're going to learn about uppercase and noUppercase, two important concepts in C++. Let's get started! š
In C++, uppercase and noUppercase refer to the conversion of characters in a string to either uppercase or lowercase, respectively.
Before we dive into uppercase and noUppercase, let's quickly review strings in C++. A string is an array of characters, terminated by a null character (\0).
char myString[] = "Hello, World!";In the above example, myString is an array of characters containing the string "Hello, World!".
C++ provides a built-in function called toupper() to convert all characters in a string to uppercase.
#include <cctype>
char myString[] = "Hello, World!";
void uppercase(char str[]) {
for (int i = 0; myString[i] != '\0'; i++) {
myString[i] = static_cast<char>(toupper(myString[i]));
}
}
int main() {
uppercase(myString);
std::cout << myString; // Output: HELLO, WORLD!
return 0;
}In the code above, we've defined a function uppercase() that converts all characters in a given string to uppercase. The toupper() function is used to convert a single character to uppercase, and we apply it to each character in the string using a for loop.
Unfortunately, C++ does not have a built-in function to convert all characters in a string to lowercase like it does for uppercase. However, we can create our own noUppercase function using a series of if-else statements.
#include <cctype>
char myString[] = "Hello, World!";
void noUppercase(char str[]) {
for (int i = 0; myString[i] != '\0'; i++) {
if (isupper(myString[i])) {
myString[i] += 32;
}
}
}
int main() {
noUppercase(myString);
std::cout << myString; // Output: hello, world!
return 0;
}In the code above, we've defined a function noUppercase() that converts all characters in a given string to lowercase. We use the isupper() function to check if a character is uppercase, and if it is, we subtract 32 from the character's ASCII value to convert it to lowercase.
Here's a complete program that demonstrates both uppercase and noUppercase functions:
#include <cctype>
char myString[] = "Hello, World!";
void uppercase(char str[]) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = static_cast<char>(toupper(str[i]));
}
}
void noUppercase(char str[]) {
for (int i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
str[i] += 32;
}
}
}
int main() {
// Original String
std::cout << "Original String: " << myString << std::endl;
// Uppercase String
uppercase(myString);
std::cout << "Uppercase String: " << myString << std::endl;
// NoUppercase String
noUppercase(myString);
std::cout << "NoUppercase String: " << myString << std::endl;
return 0;
}When you run this program, you'll see the original string, the string converted to uppercase, and the string converted to noUppercase:
Original String: Hello, World!
Uppercase String: HELLO, WORLD!
NoUppercase String: hello, world!
Which C++ built-in function is used to convert a single character to uppercase?
We've learned about uppercase and noUppercase in C++, as well as how to convert strings to uppercase and noUppercase using C++ functions. Remember, practice is key, so try experimenting with these concepts in your own code!
Happy Coding! šš¼