Case Conversion in Strings
Explore how to create functions that convert strings to lowercase and uppercase using ASCII values in C++. Understand different approaches including a special lowercase conversion that preserves the first letter of each word. Practice through challenges that enhance your string manipulation skills.
String conversion to lowercase
Let’s make a strLower() function in which we need to convert uppercase alphabets to lowercase by adding the ASCII value. For example, if the ASCII value of A is 65, simply add 32 to convert it into lowercase. If you don’t remember the ASCII value, you can add the following in the alphabet:
alphabet+'a'-'A'
Here are two challenges for you.
Exercise 1: String conversion to uppercase
Write the strUpper() function solution.
Sample input
This is A World of C++
Sample output
THIS IS A WORLD OF C++
#include <iostream>
#include <string.h>
using namespace std;
void strUpper(char S[ ])
{
//write your code here
}
int main()
{
char S[ ] = "This is A WORLD of C++";
cout << "Before strUpper function:"<<S<<endl;
strUpper(S);
cout << "After strUpper function: "<<S<<endl;
return 0;
}
Exercise 2: Special to lowercase conversion
Write the strLowerSpecial() function, which lowers the case of all the characters but not the start of the word.
Sample input
THIS IS PAKISTAN
Sample output
This Is Pakistan
#include <iostream>
#include <cstring>
using namespace std;
void strLowerSpecial(char S[ ])
{
//write your code here
}
int main()
{
char S[ ] = "THIS IS PAKISTAN";
cout << "Before lowering: "<<S<<endl;
strLowerSpecial(S);
cout << "After lowering: "<<S<<endl;
return 0;
}