SlideShare a Scribd company logo
1 of 11
Download to read offline
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
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
#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 < 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=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
#include
#include
#include
#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.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
*/

More Related Content

Similar to Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf

write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
jyothimuppasani1
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
JoeyDelaCruz22
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
mohdjakirfb
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
SyedHaroonShah4
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx
gertrudebellgrove
 

Similar to Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf (20)

Find an LCS of X = and Y = Show the c and b table. Attach File .docx
  Find an LCS of X =  and Y =   Show the c and b table.  Attach File  .docx  Find an LCS of X =  and Y =   Show the c and b table.  Attach File  .docx
Find an LCS of X = and Y = Show the c and b table. Attach File .docx
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
String searching
String searchingString searching
String searching
 
Overloading
OverloadingOverloading
Overloading
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
String .h
String .hString .h
String .h
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Link list
Link listLink list
Link list
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx
 
Strings
StringsStrings
Strings
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
Pointers
PointersPointers
Pointers
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 

More from arjunhassan8

Explain how substances get across membranes and into the cell.Sol.pdf
Explain how substances get across membranes and into the cell.Sol.pdfExplain how substances get across membranes and into the cell.Sol.pdf
Explain how substances get across membranes and into the cell.Sol.pdf
arjunhassan8
 
Does yeast have a post translation pathway for protein synthesisIf .pdf
Does yeast have a post translation pathway for protein synthesisIf .pdfDoes yeast have a post translation pathway for protein synthesisIf .pdf
Does yeast have a post translation pathway for protein synthesisIf .pdf
arjunhassan8
 
Determine whether the following statements about human ABO Wood group.pdf
Determine whether the following statements about human ABO Wood group.pdfDetermine whether the following statements about human ABO Wood group.pdf
Determine whether the following statements about human ABO Wood group.pdf
arjunhassan8
 
blackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdf
blackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdfblackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdf
blackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdf
arjunhassan8
 
Bitcoin as an Ethical Dilemma closing case (Question below article).pdf
Bitcoin as an Ethical Dilemma closing case (Question below article).pdfBitcoin as an Ethical Dilemma closing case (Question below article).pdf
Bitcoin as an Ethical Dilemma closing case (Question below article).pdf
arjunhassan8
 
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
arjunhassan8
 
This problem is based on the B92 method of quantum key distri.pdf
This problem is based on the B92 method of quantum key distri.pdfThis problem is based on the B92 method of quantum key distri.pdf
This problem is based on the B92 method of quantum key distri.pdf
arjunhassan8
 
The main difference between passive and active transport is the spec.pdf
The main difference between passive and active transport is  the spec.pdfThe main difference between passive and active transport is  the spec.pdf
The main difference between passive and active transport is the spec.pdf
arjunhassan8
 
The following table is a summary of randomly chosen student evaluati.pdf
The following table is a summary of randomly chosen student evaluati.pdfThe following table is a summary of randomly chosen student evaluati.pdf
The following table is a summary of randomly chosen student evaluati.pdf
arjunhassan8
 

More from arjunhassan8 (20)

Explain how substances get across membranes and into the cell.Sol.pdf
Explain how substances get across membranes and into the cell.Sol.pdfExplain how substances get across membranes and into the cell.Sol.pdf
Explain how substances get across membranes and into the cell.Sol.pdf
 
Does yeast have a post translation pathway for protein synthesisIf .pdf
Does yeast have a post translation pathway for protein synthesisIf .pdfDoes yeast have a post translation pathway for protein synthesisIf .pdf
Does yeast have a post translation pathway for protein synthesisIf .pdf
 
Determine whether the following statements about human ABO Wood group.pdf
Determine whether the following statements about human ABO Wood group.pdfDetermine whether the following statements about human ABO Wood group.pdf
Determine whether the following statements about human ABO Wood group.pdf
 
Describe a similarity transformation that maps trapezoid WXYZ to tr.pdf
Describe a similarity transformation that maps trapezoid WXYZ to tr.pdfDescribe a similarity transformation that maps trapezoid WXYZ to tr.pdf
Describe a similarity transformation that maps trapezoid WXYZ to tr.pdf
 
blackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdf
blackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdfblackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdf
blackboard.ecu edu Take Test Exam ll-Blou2300601201730 he map above .pdf
 
Bitcoin as an Ethical Dilemma closing case (Question below article).pdf
Bitcoin as an Ethical Dilemma closing case (Question below article).pdfBitcoin as an Ethical Dilemma closing case (Question below article).pdf
Bitcoin as an Ethical Dilemma closing case (Question below article).pdf
 
2. This seasons flu virus is spreading based on a logistic functio.pdf
2. This seasons flu virus is spreading based on a logistic functio.pdf2. This seasons flu virus is spreading based on a logistic functio.pdf
2. This seasons flu virus is spreading based on a logistic functio.pdf
 
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
 
Write the code segments to create a monitor that has the same behavi.pdf
Write the code segments to create a monitor that has the same behavi.pdfWrite the code segments to create a monitor that has the same behavi.pdf
Write the code segments to create a monitor that has the same behavi.pdf
 
Which of the following is NOT a rationale for using mRNA intermed.pdf
Which of the following is NOT a rationale for using mRNA intermed.pdfWhich of the following is NOT a rationale for using mRNA intermed.pdf
Which of the following is NOT a rationale for using mRNA intermed.pdf
 
Which elements are in series and which are in parallel in Figure P1.3.pdf
Which elements are in series and which are in parallel in Figure P1.3.pdfWhich elements are in series and which are in parallel in Figure P1.3.pdf
Which elements are in series and which are in parallel in Figure P1.3.pdf
 
What domain is horizontal gene transfer most common within Multiple .pdf
What domain is horizontal gene transfer most common within Multiple .pdfWhat domain is horizontal gene transfer most common within Multiple .pdf
What domain is horizontal gene transfer most common within Multiple .pdf
 
This problem is based on the B92 method of quantum key distri.pdf
This problem is based on the B92 method of quantum key distri.pdfThis problem is based on the B92 method of quantum key distri.pdf
This problem is based on the B92 method of quantum key distri.pdf
 
The _ operator can be used to determine a variable address The variab.pdf
The _ operator can be used to determine a variable address The variab.pdfThe _ operator can be used to determine a variable address The variab.pdf
The _ operator can be used to determine a variable address The variab.pdf
 
The main difference between passive and active transport is the spec.pdf
The main difference between passive and active transport is  the spec.pdfThe main difference between passive and active transport is  the spec.pdf
The main difference between passive and active transport is the spec.pdf
 
The opposite movement of supination is ___. Pronation flexion exte.pdf
The opposite movement of supination is ___.  Pronation  flexion  exte.pdfThe opposite movement of supination is ___.  Pronation  flexion  exte.pdf
The opposite movement of supination is ___. Pronation flexion exte.pdf
 
The latter or final stages of an RNA based heriditary world were lik.pdf
The latter or final stages of an RNA based heriditary world were lik.pdfThe latter or final stages of an RNA based heriditary world were lik.pdf
The latter or final stages of an RNA based heriditary world were lik.pdf
 
The following table is a summary of randomly chosen student evaluati.pdf
The following table is a summary of randomly chosen student evaluati.pdfThe following table is a summary of randomly chosen student evaluati.pdf
The following table is a summary of randomly chosen student evaluati.pdf
 
A female carrier of Tay-Sachs wants to have a child with a male carri.pdf
A female carrier of Tay-Sachs wants to have a child with a male carri.pdfA female carrier of Tay-Sachs wants to have a child with a male carri.pdf
A female carrier of Tay-Sachs wants to have a child with a male carri.pdf
 
So I dont understand explicit equations. Whats the formula and how.pdf
So I dont understand explicit equations. Whats the formula and how.pdfSo I dont understand explicit equations. Whats the formula and how.pdf
So I dont understand explicit equations. Whats the formula and how.pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 

Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf

  • 1. 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
  • 2. 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;
  • 3. //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()
  • 4. { 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];
  • 5. //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: ";
  • 6. //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 using namespace std; //Class definition class myString { public: //Char Array pointer char *arr; //Integer to hold length int len; //Default constructor myString(); //Parameter constructor
  • 7. 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 #include #include #include "myString.h" using namespace std; //Default Constructor myString :: myString() { //Initializing length to 0 len = 0; arr = new char[0];
  • 8. } //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()
  • 9. { //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=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 #include #include
  • 10. #include #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.print(); if(obj2.isPalindrome() == true) { cout << " is palindrome "; } else cout << " is not palindrome "; //cout << " " << obj2.isPalindrome(); cout << endl; return 0; } /* output:
  • 11. Object 1: String: madam Object 2: String: anil After reversing: String: lina String: lina is not palindrome */