-1

So I was asked to write a C++ program to implement a string object. Include member functions to compare two strings, concatenate two strings and find out whether the strings are palindrome or not.

This is my Code.

#include <iostream>
#include<string.h>
using namespace std;

class STR
{
    public:
        void concatenateFunction(string s1, string s2);
        void compareFunction(string s1, string s2) ;
        void palindromeFunction(char str[]);
};

void STR::compareFunction(string s1, string s2)
{
    // comparing both using inbuilt function
    int x = s1.compare(s2);
    cout<< "Compare:\n";
    if (x != 0)
        cout << s1 << " is not equal to "
             << s2 << endl;
    if (x > 0)
        cout << s1 << " is greater than "
             << s2 << endl;
    else
        cout << s2 << " is greater than "
             << s1 << "\n" << endl;
}

void STR::concatenateFunction(string s1, string s2, char c)
{
    string conc = s1 + s2;
    cout << "Concatenate:\n" << conc << "\n";

    std::string str = conc;

    char* c = strcpy(new char[str.length() + 1], str.c_str());
    return;
}

void STR:: palindromeFunction(char str[])
{
    int l = 0;
    int h = strlen(str) - 1;

    while (h > l)
    {
        if (str[l++] != str[h--])
        {
            cout << str << " is Not Palindrome";
            return;
        }
    }
    cout << str << " is Palindrome";
}

int main()
{
    STR res;
    char c;
    string s1,s2,conc;
    cout << "Enter a string 1: ";
    getline(cin, s1);

    cout << "Enter a string 2: ";
    getline(cin, s2);

    res.compareFunction(s1, s2);
    res.concatenateFunction(s1, s2);
    return 0;
}

If you see in the concatenateFunction i'm trying to get the concatenate as a character so that i can perform the palindrome function. but as it is a std its showing error. So is their any workaround to make my code run. Please do help.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Please indicate the exact line with the error, and the error message. Also, why are you using `new` and `strcpy`? The `std::string` container has better facilities. – cigien Aug 20 '20 at 13:23
  • Your functions return void and just print. This is probably not what is intended. – Amir Kirsh Aug 20 '20 at 13:25
  • Neither the question nor the code have anything to do with OOP. – Konrad Rudolph Aug 20 '20 at 13:25
  • Your class has no members, the functions could be `static` – Amir Kirsh Aug 20 '20 at 13:26
  • strcpy was me attempting different methods to convert it into character @cigien – AaronStoneA13 Aug 20 '20 at 13:27
  • 3
    Your class does not represent a string object, it's just a collection of functions, it has no data. Forget your actual question, you need to understand the task you've been given, because what you have at present will be a fail. – john Aug 20 '20 at 13:28
  • @KonradRudolph Well it came from this subject. – AaronStoneA13 Aug 20 '20 at 13:28
  • @john in the pandemic we were given only notes so i'm following it as much as possible. And the only thing i'm stuck is the palindrome one. – AaronStoneA13 Aug 20 '20 at 13:30
  • Your `STR` class should contain data representing a string. That's what `implement a string object` means. – john Aug 20 '20 at 13:31
  • @AaronStoneA13 Well I can't see your notes, but none of the function you've written so far are correct because they do not implement a string object. They are string functions, but that is not the same thing. And no doubt you could write a palindrome string function, but that is not what you've been asked to do. You been asked to implement a string object. – john Aug 20 '20 at 13:33
  • Do you have in the notes something about data members? Constructors? Destructors? – Amir Kirsh Aug 20 '20 at 13:33
  • `std::string` is the kind of string-representing class you should implement. Your member functions should not print anything but return things. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). (The point of exercises is almost never to produce a specific output, but to practice a specific method or technique. And becoming familiar with terminology.) – molbdnilo Aug 20 '20 at 13:46

2 Answers2

1

This is probably not the string class that you were requested to implement.

Functions are void and just print.

The class has no members. So it is not so OOP but rather a different way to write procedural code.

Amir Kirsh
  • 12,564
  • 41
  • 74
  • Thanks for your comment. Atleast a clear answer. So do you have any sites you can recommend – AaronStoneA13 Aug 20 '20 at 13:31
  • Do you not have a text book? There are some recommendations [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – john Aug 20 '20 at 13:35
  • @AaronStoneA13 take a look at this one, read not only the question but also the answer: https://codereview.stackexchange.com/questions/98329/stdstring-implementation and try to figure out the basic things from your notes about classes. Good luck. – Amir Kirsh Aug 20 '20 at 13:36
1

Your class should be a type that represents a string - like a very basic version of std::string.

It should look something like this

class STR
{
    public:
        STR(const char* s);
        STR(const STR&);
        ~STR();
        STR& operator=(const STR& s);
        // Return the concatenation of this and 's'.
        STR concatenate(const STR& s) const;
        // Return -1, 0, or 1 if this is smaller, equal, or greater than 's' (like `strcmp`).
        int compare(const STR& s) const;
        // Retrun wheter this string is a palindrome.
        bool is_palindrome() const;
    private:
      // Suitable member variables.
};

And you would use it like this:

int main()
{
    STR s1("hello");
    STR s2("goodbye");
    STR s3 = s1.concatenate(s2);
    if (s3.compare("hellogoodbye") == 0)
        std::cout << "good\n";
    else
        std::cout << "not good\n";
   std::cout << "Is s1 a palindrome? " << s1.is_palindrome();
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82