C++ problem. I have this main.cpp and string.h files to check if a string is a p
ID: 3749914 • Letter: C
Question
C++ problem. I have this main.cpp and string.h files to check if a string is a palindrome or not . example Output is below.
I am getting a terminate called after throwing an instance of 'int' , Aborted , this happens after i do g++ main.cpp "avid diva". How do I fix this, or where do i get started to prevent this. Below is the main.cpp file followed by the string.h file. I am creating my own string class.
#include <cstdlib> // exit
#include <cctype> // tolower
#include <iostream> // cout
#include "string.h" // String class
using namespace std;
bool isPalindrome(String A){
int i, length=0;
//gets char length
int j = 0;
while (A[j] != '') {
++j;
}
length = j;
// palindrome function
for(i=0;i < length ;++i){
if(A[i] != A[length-i-1]){
return false;
break;
}
}
return true;
}
int main(int argc, char *argv[]) {
// check for the right number of cmd args
// and exit if not properly included
if (argc != 2) {
cout << "You must enter one string as an argument!" << endl;
exit(1);
}
// Here's the command line argument string
String stringA(argv[1]);
// check if it's a palindrome
if (isPalindrome(stringA)) {
// if so, then print that fact
cout << stringA << " is a palindrome" << endl;
// append the reversed string
// stringA.append(stringA.reversed());
// and we have another palindrome
cout << "so is " << stringA << endl;;
} else {
// otherwise, print that fact
cout << stringA << " is not a palindrome" << endl;
// append the reversed string
// stringA.append(stringA.reversed());
// and magically we have a palindrome from a non-palindrome!
cout << "but " << stringA << " is" << endl;
}
return 0;
}
string.h file
#ifndef string
#define string
class String {
public:
String(); // create an empty String object
String(const char *); // create a String object from a C-style string
String (const char); // create a String object from a single char
int length() const; // return the number of characters in the string (not including null-termination)
~String (); // destructor of the class, frees heap memory
void append(const String &); // insert characters at the end of the String but before the null-term
String reversed() const; // return a new String object with the characters in reverse order
char & operator[](int) const; // access the character at a given index
const char * c_str() const; // return a C-style string with the same characters in the same order
friend std::ostream & operator<<(std::ostream &, const String &); // allow for use with cout
char* info;
int size;
};
int String:: length() const{
/* int j = 0;
while (info[j] != '') {
++j;
}
size = j;*/
return size;
}
std::ostream & operator<<(std::ostream & out, const String &S){
if (S.length() > 0) {
for (int i=0; i < S.length(); i++)
out << S[i];
} else out << "";
return out;
}
String String:: reversed() const{
//void backword(char word[20])
String word (info);
int i=0,j=0;
char temp;
j= word.size -1;
// j=size(word)-1;
while(i<j) {
temp=word[i];
word[i]=word[j];
word[j]=temp;
i++;
j--;
}
return word;
}
String:: String(){
size = 0;
info = new char[0];
}
String:: String(const char C){
size= 1;
info = new char[sizeof(char) * 1];
info[0] = C;
/* adding null-character to make it CStyle string */
info[1] = '';
}
String:: String(const char *C){
if (C) {
int m = 0;
while (C[m] != '') m++;
size = m;
info = new char[size * sizeof(char)];
for (int i=0; i < m; i++){
info[i] = C[i];
}
info[size] = '';
} else {
size = 0;
info = new char[0];
info[0] = '';
}
}
char & String::operator[] (int i) const {
if (i >= size) throw 1;
return info[i];
}
const char * String::c_str() const {
/* type cast to const char * and return it */
return static_cast<const char *>(info);
}
String::~String() {
/* freeing allocated memory by new keyword */
if (info) {
delete info;
}
}
void String::append(const String & obj) {
if (info) {
int new_len = obj.size + size;
char * new_info = new char[new_len];
int i;
for (i = 0; i < size; ++i) {
new_info[i] = info[i];
}
for (i = 0; i < obj.size; ++i) {
new_info[size + i] = obj[i];
}
new_info[new_len] = '';
delete info;
info = new_info;
}
}
#endif
Explanation / Answer
change this function in String.h file to this code:
char & String::operator[] (int i) const {
if (i > size) throw 1;
return info[i];
}
please give thumbs up, thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.