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

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

Example:

Input:
Enter String: sHuBh PaChOrI

Output:
Title cased String: Shubh Pachori

C++ code to change the string to title 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);
    }

  // titleCase() function to convert 
  // the string to title case
  string titleCase() {
    // char type array to store title cased 
    // string
    char temp[30];

    // int type variable for indexing
    int index;

    // for loop for traversing of whole string
    for (index = 0; str[index]; index++) {
      // if condition for the first character 
      // of the string
      if (index == 0) {
        // if condition to check if it is a alphabet
        if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] >= 'A' && str[index] <= 'Z')) {

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

            // then convert it to uppercase and 
            // store it in temp string
            temp[index] = str[index] - 32;
          }
          // else it is copied as it is
          else {
            temp[index] = str[index];
          }
        }
        // else it is copied as it is
        else {
          temp[index] = str[index];
        }
      }

      // else if condition for spaces at index
      else if (str[index] == ' ') {
        // copying space in temp string
        temp[index] = str[index];

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

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

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

            // then convert it to uppercase and 
            // store it in temp string
            temp[index] = str[index] - 32;
          }

          // else it is copied as it is
          else {
            temp[index] = str[index];
          }
        }
        // else it is copied as it is
        else {
          temp[index] = str[index];
        }
      }
      // else if condition to check if it is a alphabet
      else if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] >= 'A' && str[index] <= 'Z')) {

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

          // then convert it to loweercase and 
          // store it in temp string
          temp[index] = str[index] + 32;
        }

        // else it is copied as it is
        else {
          temp[index] = str[index];
        }
      }
      // else it is copied as it is
      else {
        temp[index] = str[index];
      }
    }

    // transfering null in the temp string
    temp[index] = 0;

    // returning 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 
  // title case string
  string str;

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

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

  return 0;
}

Output:

RUN 1:
Enter String:hello world
Title cased String:Hello World

RUN 2:
Enter String:TiTlE cAsE sTrInG 
Title cased String:Title Case 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 titleCase() 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 titleCase() member function to change the given string to title case. The titleCase () function contains the logic to change the given string to title 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.