QUESTION DESCRIPTION 

Kamal is struggling to convert the characters of given string to upper case.

Help Kamal to convert the given string to upper case. Refer the following sample test cases.

Solution:

#include <iostream>
using namespace std;
void upper_string(string str)
{
  for(int i=0;str[i]!='\0';i++)
  {
  if(str[i] >= 'a' && str[i] <= 'z')
    str[i]=str[i]-32;
  }
cout<<str;
}
int main()
{
  string str;
  getline(cin,str);
  upper_string(str);

return 0;

}

Converting a String to Upper Case in C++

In this task, Kamal needs help converting the characters of a given string to upper case. The provided C++ code performs this operation by iterating through each character of the string and converting lowercase letters to uppercase.

Problem Description

You need to write a C++ program that:

  1. Takes a string input from the user.
  2. Converts all lowercase letters in the string to uppercase.
  3. Outputs the converted string.

How the Code Works

  1. Input Handling: The getline(cin, str) function reads the entire line of input into the string str, allowing for spaces within the string.
  2. Character Conversion:
    • The upper_string function iterates through each character of the string.
    • It checks if the character is a lowercase letter ('a' to 'z').
    • If it is, the character is converted to uppercase by subtracting 32 from its ASCII value. This works because the difference between the ASCII values of corresponding uppercase and lowercase letters is 32.
  3. Output: The modified string is printed after all characters have been processed.

Sample Test Cases

Test Case 1:

  • Input: hello world
  • Output: HELLO WORLD
  • Reason: All lowercase letters are converted to their uppercase counterparts.

Test Case 2:

  • Input: C++ Programming
  • Output: C++ PROGRAMMING
  • Reason: Only the lowercase letters are converted to uppercase.