×

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 find the product of all integers in a string using class

Submitted by Shubh Pachori, on September 01, 2022

Problem statement

Given a string, we have to find the product of all integers from it using the class and object approach.

Example:

Input:
Enter String: multiply 12 and 13

Output:
Product of all integers in String is 156

C++ code to find the product of all integers in a string using the class and object approach

#include <bits/stdc++.h>
using namespace std;

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

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

  // productString() function to multiply all numerical 
  // values present in the string
  int productString() {
    // initializing int type variables 
    // to perform operations
    int index, number, product = 1;

    // for loop to traverse the whole string
    for (index = 0; str[index]; index++) {
      // if condition to check if the character at
      // index is a numerical or not
      if (isdigit(str[index])) {
        continue;
      } else {
        str[index] = ' ';
      }
    }

    // stringstream allows us to read through
    // the inserted string str
    stringstream temp(str);

    // while to add all numerical values in 
    // the inserted string
    while (temp >> number) {
      product = product * number;
    }

    // returning product
    return product;
  }
};

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

  // int type variable to store the product
  int product;

  // getString() function to store the String
  S.getString();

  // productString() function to multiply all
  // numerical values in the string
  product = S.productString();

  cout << "Product of all integers in String is " << product;

  return 0;
}

Output

RUN 1:
Enter String: Numbers are 10 and 8
Product of all integers in String is 80

RUN 2:
Enter String: hi 5 its 11
Product of all integers in String is 55

Explanation

In the above code, we have created a class named String, a string type data member str to store the string, and public member functions getString() and productString() to store and to find out the product of all integers in the string.

In the main() function, we are creating an object S of class String, reading the string by the user using getString() function, and finally calling the productString() member function to find out the product of all integers in the string. The productString() function contains the logic to find out the product of all integers 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.