×

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 identify missing character in string using class

Submitted by Shubh Pachori, on September 08, 2022

Problem statement

Given a string, we have to identify the missing character in string using the class and object approach.

Example:

Input:
Enter String : abcdeg

Output:
Missing Character : f

C++ code to identify missing character in string using the class and object approach

#include <iostream>
using namespace std;

// create a class
class String {
  // private data member
 private:
  string str;

  // public member functions
 public:
  // getString() function to store string
  void getString() {
    cout << "Enter String: ";
    getline(cin, str);
  }

  // missingString() function to check for
  // the missing Character
  char missingString() {
    // initializing variables to 
    // perform operations
    int lower[256];
    int upper[26];
    int length, index;
    char miss;

    // for loop to remove garbage values
    // form these two arrays
    for (index = 0; index < 26; index++) {
      lower[index] = 0;
      upper[index] = 0;
    }

    // caluclating the length of the string
    for (length = 0; str[length]; length++)
      ;

    // for loop to separate lowercase and 
    // uppercase characters
    for (index = 0; index < length; index++) {
      // if condition for uppercase characters
      if (str[index] > 90) {
        lower[str[index] % 'a'] = 1;
      }

      // else for lowercase characters
      else {
        upper[str[index] % 'A'] = 1;
      }
    }

    // for loop to convert uppercase to lowercase
    for (index = 0; index < 26; index++) {
      lower[index] = lower[index] | upper[index];
    }

    // for loop to check for missing character in string
    for (index = 1; index < 25; index++) {
      if (lower[index - 1] == 1 && lower[index] == 0 && lower[index + 1]) {
        miss = 'a' + index;

        return miss;
      }
    }

    return '$';
  }
};

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

  // string variable to store the string
  string str;

  // calling getString() function to 
  // input string
  S.getString();

  // calling missingString() function to 
  // check missing Character
  str = S.missingString();

  cout << "Missing Character : " << str << endl;

  return 0;
}

Output

RUN 1:
Enter String: abcdefhij
Missing Character : g

RUN 2:
Enter String: abcdfg
Missing Character : e

Explanation

In the above code, we have created a class String, one string type data members str to store the string, and public member functions getString() and missingString() to store the string and to check for the missing character in the string.

In the main() function, we are creating an object S of class String, reading the inputted string by the user using getString() function, and finally calling the missingString() member function to check for the missing character in the string. The missingString() function contains the logic to check for the missing character in the string and printing the result.

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

Comments and Discussions!

Load comments ↻





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