C++ program to change the string to toggle case using class

Given a string, we have change it to toggle case using class and object approach.
Submitted by Shubh Pachori, on August 25, 2022

Example:

Input:
Enter String: sHuBh

Output:
Toggle cased String: sHUBH

C++ code to change the string to toggle case using the class and object approach

#include <iostream>
using namespace std;

// create a class
class String {
  // private data member
  private:
    char str[30];

  // public functions
  public:
    // getString() function to get the string
    void getString() {
      cout << "Enter String:";
      cin.getline(str, 30);
    }

  // toggleCase() function to convert string 
  // to toggle case
  string toggleCase() {

    // char type array to store toggle case string
    char temp[30];

    // int type variable for indexing
    int index;

    // for loop for traversing the whole string str
    for (index = 0; str[index]; index++) {

      // if condition for the first character of the string
      if (index == 0) {
        // if condition to check if the first 
        // character is a alphabet or not
        if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] >= 'A' && str[index] <= 'Z')) {

          // if condition to check if the first 
          // character is a uppercase alphabet
          if ((str[index] >= 'A' && str[index] <= 'Z')) {

            // then we will convert it to lowercase and 
            // copy it in the temp string
            temp[index] = str[index] + 32;
          }
          // else copy as it is in temp string
          else {
            temp[index] = str[index];
          }
        }

        // else copy as it is in temp string
        else {
          temp[index] = str[index];
        }
      }

      // else if it is a space at index
      else if (str[index] == ' ') {

        // copying space in the temp string
        temp[index] = str[index];

        // increment in index of 1 to check next term
        index++;

        // if condition to check if the first 
        // character is a alphabet or not
        if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] >= 'A' && str[index] <= 'Z')) {

          // if condition to check if the first 
          // character is a uppercase alphabet
          if ((str[index] >= 'A' && str[index] <= 'Z')) {

            // then we will convert it to lowercase and 
            // copy it in the temp string
            temp[index] = str[index] + 32;
          }

          // else copy as it is in temp string
          else {
            temp[index] = str[index];
          }

        }
        // else copy as it is in temp string
        else {
          temp[index] = str[index];
        }
      }

      // else if condition to check if the first 
      // character is a alphabet or not at index
      else if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] >= 'A' && str[index] <= 'Z')) {

        // if condition to check if the first 
        // character is a lowercase alphabet
        if ((str[index] >= 'a' && str[index] <= 'z')) {

          // then we will convert it to uppercase and 
          // copy it in the temp string
          temp[index] = str[index] - 32;
        }

        // else copy as it is in temp string
        else {
          temp[index] = str[index];
        }
      }

      // else copy as it is in temp string
      else {
        temp[index] = str[index];
      }
    }

    // transfering nul in temp string
    temp[index] = 0;

    // returnimg temp string
    return temp;
  }
};

int main() {
  // create an object
  String S;

  // function is called by the 
  // object to store the string
  S.getString();

  // string type variable to store string
  string str;

  // toggleCase() function is called by the object 
  // to convert the string to toggle case
  str = S.toggleCase();

  cout << "Toggle cased String:" << str;

  return 0;
}

Output:

Enter String: sTrInG
Toggle cased String: sTRING

Explanation:

In the above code, we have created a class String, one char type array data member str[30] to store the string, and public member functions getString() and toggleCase() to store and manipulate the given string.

In the main() function, we are creating an object S of class String, reading a string by the user using the function getString(), and finally calling the toggleCase() member function to change the given string to toggle case. The toggleCase () function contains the logic to change the given string to the toggle case and printing the result.

C++ Class and Object Programs (Set 2) »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.