Simple Data Class using C++
Posted by Samath
Last Updated: January 07, 2021

Design a class Data using the description of the data members and member functions given below:

Class name :                   Data

Data members
str :                                  string variable of protected type to store a sentence.
len :                                 To Store the length of str

Member functions
Data() :                            constructor to assign blank to str.
void acceptstr() :              to read a sentence into str
void print() :                      to print value of str with suitable heading.
void removeDuplicate() :  to replace the duplicate characters in sequence by its single occurrence in the string str. For example “Heee iiiiss ggoiinggg hooommmeee”. The output should be “He is going home”. Note that the sequence of spaces should also be replaced by single space. Print the modified sentence.

Code:

#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
 
using namespace std;
 
class Data
{
	private:
		int len;
		string str;
 
	public:
		Data();
		void acceptstr(string s);
		void print();
		void removeDuplicate();
};
 
Data::Data()
{
	str = "";
}
 
void Data::removeDuplicate()
{
 
	char* strdup = new char[str.length() +1];
	 int tail = 1;
    int i, j;
 
	strdup[str.size()]=0;
	memcpy(strdup,str.c_str(),str.size());
 
 
    if(strdup == NULL)
        return;
 
    char *tmp = strdup;
 
    while(*tmp){
        tmp ++;
    }
 
    int len = tmp - strdup;
    if(len < 2)
        return;
 
 
    for(i = 1; i < len; i++)
	{
        for(j = 0; j < tail; j++)
		{
			if(strdup[i] != strdup[i - 1])
			{
				continue;
			}
            if(strdup[i] == strdup[j])
			{
                break;
            }
        }
 
        if(j == tail)
		{
            strdup[tail] = strdup[i];
            tail ++;
        }
    }
 
    strdup[tail] = '\0';
 
   string mystring = string(strdup);
 
   str = mystring;
 
    cout<<"Modified Sentense: "<<str<<endl;
}
 
void Data::print()
{
	cout<<"String Value: "<<str<<endl;
}
 
void Data::acceptstr(string s)
{
	str = s;
}
 
int main()
{
	string newstr;
	Data info;
 
	cout<<"Please enter new string: ";
	getline(cin,newstr);
	info.acceptstr(newstr);
 
	info.removeDuplicate();
	system("pause");
	return 0;
}
Related Content
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024
Square and Cube Class in C++
Square and Cube Class in C++
Samath | Jan 01, 2015