×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to reverse name and surname using class

Submitted by Shubh Pachori, on September 05, 2022

Problem statement

Given a string (first and last name), we have to reverse name and surname using the class and object approach.

Example:

Input:
Enter Name : james bond

Output:
Name : bond james

C++ code to reverse name and surname using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Name {
  // private data member
 private:
  char name[30];

  // public member functions
 public:
  // getName() function to insert name
  void getName() {
    cout << "Enter Name : ";
    cin.getline(name, 30);
  }

  // swapName() function to 
  // swap name and surname
  string swapName() {
    // initializing some variables to 
    // perform operations
    int index, index_2 = 0;
    char temp[30];

    // for loop to traverse the whole name 
    // with surname
    for (index = 0; name[index]; index++) {
      // if condition to check space
      if (name[index] == ' ') {
        // increment of 1 in index
        index++;

        // for loop to copy characters 
        // from name to temp string
        for (index_2 = 0; name[index]; index++, index_2++) {
          temp[index_2] = name[index];
        }
        // break the statement
        break;
      }
    }

    // copying space in between
    temp[index_2] = ' ';

    // increment of 1 in index_2
    index_2++;

    // for loop to traverse the rest string
    for (index = 0; name[index]; index_2++, index++) {
      // copying the rest string in temp
      temp[index_2] = name[index];

      // if condition to check space
      if (name[index] == 32) {
        // break the statement
        break;
      }
    }

    // transfering null at th end
    temp[index_2] = 0;

    // returning the string
    return temp;
  }
};

int main() {
  // create object
  Name N;

  // string type variable to 
  // store the name
  string name;

  // calling getName() function to 
  // insert the name
  N.getName();

  // calling swapName() function to 
  // swap name and surname
  name = N.swapName();

  cout << "Name : " << name << endl;

  return 0;
}

Output

RUN 1:
Enter Name : Ram Chandra
Name : Chandra Ram

RUN 2:
Enter Name : Alvin Alexander
Name : Alexander Alvin

Explanation

In the above code, we have created a class Name, one char type array data member name[30] to store the name, and public member functions getName() and swapName() to store the name and to swap it.

In the main() function, we are creating an object N of class Name, reading the inputted name by the user using getName() function, and finally calling the swapName() member function to swap the name and surname. The swapName() function contains the logic to swap the name and surname and printing the result.

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

Comments and Discussions!

Load comments ↻





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