Can anyone fix this code I use Visual Studios The Errors im
Can anyone fix this code, I use Visual Studios:
The Errors im getting is that it is not reverseing correctly,
And the line if (arr[i] == strRev[i])
gives me a error code C4700 uninitialized local variable \'strRev\' used
This is the program that neeeds to be created:
In C++, with a myString.h, myStringImp.cpp, and main.cpp:
Design and implement a class myString:
-Use a dynamic array of char to store the characters of myString.
-Use an int to keep track of the length of myString.
-Provide a parameterized constructor that takes an initial string, a default constructor, destructor, assignment operator, and copy constructor. All need to correctly handle the dynamic array.
-Also provide these member functions:
--length() //the length of myString
--isEqual() //if it is equal to another myString
--print() //output to a terminal cout or outFile
--reverse() //reverse the order of characters in myString
--isPalindrome() //if myString reads the same after being reversed
Here is the code that is broken:
C++ Code:
Header File: myString.h
#ifndef MYSTRING_H_INCLUDED
 #define MYSTRING_H_INCLUDED
#include
using namespace std;
//Class definition
 class myString
 {
 public:
 //Char Array pointer
 char *arr;
 //Integer to hold length
 int len;
//Default constructor
 myString();
//Parameter constructor
 myString(string str);
//Copy constructor
 myString(const myString &obj);
//Overloading assignment operator
 void operator=(const myString &obj);
//Destructor
 ~myString();
//Returns length
 int length();
//Compares two string for equality
 bool isEqual(myString &obj);
//Printing string to console
 void print();
//Reversing the string
 void reverse();
//Check for palindrome of current string and its reverse
 bool isPalindrome();
 };
#endif // MYSTRING_H_INCLUDED
Implementation File: myString.cpp
/* Implementation File */
#include
 #include
#include \"myString.h\"
using namespace std;
//Default Constructor
 myString :: myString()
 {
 //Initializing length to 0
 len = 0;
 arr = new char[0];
 }
//Parameter constructor
 myString :: myString(string str)
 {
 int i;
//Taking length
 len = strlen(str.c_str());
//Allocating space
 arr = new char[len+1];
//Copying characters
 for(i=0; i arr[i] = str[i];
arr[i] = \'\\0\';
 }
//Copy constructor
 myString :: myString(const myString &obj)
 {
 //Fetching length
 len = obj.len;
//Allocating memory
 arr = new char[len];
//Assigning dynamic character arrays
 *arr = *obj.arr;
 }
//Assignment operator overloading
 void myString :: operator=(const myString &obj)
 {
 //Fetching length
 len = obj.len;
//Allocating memory
 arr = new char[len];
//Assigning dynamic character arrays
 *arr = *obj.arr;
 }
//Destructor
 myString :: ~myString()
 {
 //deallocates space
 delete arr;
 }
//Function that returns length of string
 int myString ::length()
 {
 return len;
 }
//Compares two strings and returns true if they are equal else false
 bool myString::isEqual(myString &obj)
 {
 int i, cnt=0;
//Comparing lengths
 if(len != (obj.len))
 return false;
//Comparing character by character
 for(i=0; i {
 if(arr[i] == obj.arr[i])
 cnt++;
 }
//Return value
 if(cnt == len)
 return true;
 else
 return false;
 }
//Function that prints the string to console
 void myString::print()
 {
 int i;
 cout << \"\ \  String: \";
//Printing character by character
 for(i=0; i cout << arr[i];
cout << \" \ \";
 }
//Function that reverses the string
 void myString::reverse()
 {
 int i,j;
 char *tmp;
 tmp = new char[len];
//Reversing the array
 for(i=len-1,j=0; i>=0; i--,j++)
 tmp[j] = arr[i];
//Reallocating string
 *arr = *tmp;
 }
//Function that checks for palindrome of a string and its reverse
 bool myString::isPalindrome()
 {
 char *strRev;
int i, cnt=0;
//Creating a string object
 myString temp(arr);
//REversing the string
 temp.reverse();
//Comparing character by character
 for(i=0; i {
 //Comparing char in both arrays
 if(arr[i] == strRev[i])
 cnt++;
 }
//If all are matched return true else false
 if(cnt == len)
 return true;
 else
 return false;
 }
Main File: main.cpp
#include
#include \"myString.h\"
using namespace std;
int main()
 {
 //creating myString object 1
 myString obj1(\"madam\");
cout << \"\ \  Object 1: \";
 //Printing object 1
 obj1.print();
//creating myString object 2
 myString obj2(\"anil\");
cout << \"\ \  Object 2: \";
 //Printing object 2
 obj2.print();
//Reversing object 2
 obj2.reverse();
//Printing object 2 after reversing
 cout << \"\  After reversing: \";
 obj2.print();
//Checking for palindrome of object 2 with its reverse
 cout << \"\  \" << obj2.isPalindrome();
cout << endl;
 return 0;
 }
Solution
//Header File: myString.h
 #ifndef MYSTRING_H_INCLUDED
 #define MYSTRING_H_INCLUDED
 #include <string.h>
 using namespace std;
 //Class definition
 class myString
 {
 public:
 //Char Array pointer
 char *arr;
 //Integer to hold length
 int len;
 //Default constructor
 myString();
 //Parameter constructor
 myString(string str);
 //Copy constructor
 myString(const myString &obj);
 //Overloading assignment operator
 void operator=(const myString &obj);
 //Destructor
 ~myString();
 //Returns length
 int length();
 //Compares two string for equality
 bool isEqual(myString &obj);
 //Printing string to console
 void print();
 //Reversing the string
 void reverse();
 //Check for palindrome of current string and its reverse
 bool isPalindrome();
 };
 #endif // MYSTRING_H_INCLUDED
 
//Implementation File: myString.cpp
 /* Implementation File */
 #include <iostream>
 #include <string>
 #include <fstream>
 #include <iomanip>
 #include <cctype>
 #include \"myString.h\"
 using namespace std;
 //Default Constructor
 myString :: myString()
 {
 //Initializing length to 0
 len = 0;
 arr = new char[0];
 }
 //Parameter constructor
 myString :: myString(string str)
 {
 int i;
 //Taking length
 len = strlen(str.c_str());
 //Allocating space
 arr = new char[len+1];
 //Copying characters
 for(i=0; i < len+1; i++)
 arr[i] = str[i];
 arr[i] = \'\\0\';
 }
//Copy constructor
 myString :: myString(const myString &obj)
 {
 //Fetching length
 len = obj.len;
 //Allocating memory
 arr = new char[len];
 //Assigning dynamic character arrays
 *arr = *obj.arr;
 }
 //Assignment operator overloading
 void myString :: operator=(const myString &obj)
 {
 //Fetching length
 len = obj.len;
 //Allocating memory
 arr = new char[len];
 //Assigning dynamic character arrays
 *arr = *obj.arr;
 }
 //Destructor
 myString :: ~myString()
 {
 //deallocates space
 delete arr;
 }
 //Function that returns length of string
 int myString ::length()
 {
 return len;
 }
 //Compares two strings and returns true if they are equal else false
 bool myString::isEqual(myString &obj)
 {
 int i, cnt=0;
 //Comparing lengths
 if(len != (obj.len))
 return false;
 //Comparing character by character
 for(i=0; i<len;i++) {
 if(arr[i] == obj.arr[i])
 cnt++;
 }
 //Return value
 if(cnt == len)
 return true;
 else
 return false;
 }
 //Function that prints the string to console
 void myString::print()
 {
 int i;
 cout << \"\ \  String: \";
 //Printing character by character
 for(i=0; i<len;i++)
 cout << arr[i];
      
 cout << \"\ \";
 }
 //Function that reverses the string
 void myString::reverse()
 {
 int i,j;
 char *tmp;
 tmp = new char[len];
 //Reversing the array
 for(i=len-1,j=0; i>=0; i--,j++)
 tmp[j] = arr[i];
//Reallocating string
 strcpy(arr,tmp);
}
 //Function that checks for palindrome of a string and its reverse
 bool myString::isPalindrome()
 {
int i,j;
 int cnt=0;
 char *tmp;
 tmp = new char[len];
 //Reversing the array
 for(i=len-1,j=0; i>=0; i--,j++)
 tmp[j] = arr[i];
//Comparing character by character
 for(i=0; i<len;i++) {
//Comparing char in both arrays
 if(arr[i] == tmp[i])
 cnt++;
 }
 //If all are matched return true else false
 if(cnt == len)
 return true;
 else
 return false;
 }
 
 
 //Main File: main.cpp
#include <iostream>
 #include <string>
 #include <fstream>
 #include <iomanip>
 #include <cctype>
 #include \"myString.h\"
using namespace std;
 int main()
 {
 //creating myString object 1
 myString obj1(\"madam\");
 cout << \"\ \  Object 1: \";
 //Printing object 1
 obj1.print();
 //creating myString object 2
 myString obj2(\"anil\");
 cout << \"\ \  Object 2: \";
 //Printing object 2
 obj2.print();
 //Reversing object 2
 obj2.reverse();
 //Printing object 2 after reversing
 cout << \"\  After reversing: \";
 obj2.print();
//Checking for palindrome of object 2 with its reverse
 cout << \"\ \";
 obj2.print();
 if(obj2.isPalindrome() == true)
 {
 cout << \" is palindrome\ \";
 }
 else
 cout << \" is not palindrome\ \";
 //cout << \"\  \" << obj2.isPalindrome();
 cout << endl;
 return 0;
 }
/*
 output:
Object 1:
String: madam
 Object 2:
String: anil
After reversing:
String: lina
String: lina
 is not palindrome
*/
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-0.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-1.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-2.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-3.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-4.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-5.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-6.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-7.webp)
![Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e Can anyone fix this code, I use Visual Studios: The Errors im getting is that it is not reverseing correctly, And the line if (arr[i] == strRev[i]) gives me a e](/WebImages/30/can-anyone-fix-this-code-i-use-visual-studios-the-errors-im-1083727-1761569506-8.webp)
