C++ Uppercase and NoUppercase šŸŽÆ

beginner
10 min

C++ Uppercase and NoUppercase šŸŽÆ

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! šŸš€

Uppercase and NoUppercase: What's the Difference? šŸ“

In C++, uppercase and noUppercase refer to the conversion of characters in a string to either uppercase or lowercase, respectively.

  • Uppercase converts all the characters in a string to uppercase.
  • NoUppercase leaves the characters in a string as they are, without converting them to uppercase.

Understanding Strings in C++ šŸ’”

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).

cpp
char myString[] = "Hello, World!";

In the above example, myString is an array of characters containing the string "Hello, World!".

Uppercase Function in C++ šŸ’”

C++ provides a built-in function called toupper() to convert all characters in a string to uppercase.

cpp
#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.

NoUppercase Function in C++ šŸ’”

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.

cpp
#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.

Putting it All Together āœ…

Here's a complete program that demonstrates both uppercase and noUppercase functions:

cpp
#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!

Quiz Time šŸŽÆ

Quick Quiz
Question 1 of 1

Which C++ built-in function is used to convert a single character to uppercase?

Wrapping Up šŸ“

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! šŸ––šŸ¼