13

Vector of Characters (Id-1498)

Bonus Practice Sheet Winter 2018 [17-Mar-2018 to 7-Apr-2018]

Program ID- 1498

Design a class charVector that has a character vector as datamember. Provide member functions in the class to createVector, duplicateVector, duplicateRevVector and print. Functions shall be defined as follows: initializeVector – read a string and create a vector of characters duplicateVector – Add the content of the vector once at the end. For example if the content of charVector is “bat” then after the function is called the content must “batbat” duplicateRevVector – Add the content of the vector in reverse at the end. For example if the content of charVector is “bat” then after the function is called the content must “battab” print – Print content of vector, use iterators for traversal Use the vector class defined in STL for the implementation. Use [] operator in functions duplicateVector, duplicateRevVector and use iterator in print and initializeVector functions.

Code

Please Comment Working if the code worked to you

If you have other working codes please comment the codes enclosing with <pre> and </pre> 🙂

Example: <pre> Your Code </pre>

C++ Code 

Follow for important news/announcements in VIT
The Above Codes are for reference only. It doesn't mean everyone to directly copy/paste those codes.

 

Click Here to Leave a Comment Below 13 comments
Tarunya - April 1, 2018

this code isn’t working.

7
3

Reply
abc - April 2, 2018

please upload the correct code.

5
1

Reply
suraj - April 5, 2018

PLease upload the correct code broh the code is not working……………………

1
1

Reply
satya - March 27, 2020

please upload the correct code broo
code is not working

Reply
satya - March 27, 2020

please upload the correct code

Reply
jignesh singh - March 27, 2020

incorrect code

Reply
AYUSH SINGH - March 30, 2020

*The correct code for the problem stated above:*

#include
#include
#include

using namespace std;

class charVector{
vector cv;
public:
void initializeVector(string);
void dupVector();
void dupRevVector();
void print();
};

void charVector :: initializeVector(string s)
{
for (int p=0; p<s.size(); p++)
{
cv.push_back(s[p]);
}
}

void charVector :: dupVector()
{
int l = cv.size();
for (int i=0; i=0; i–)
{
cv.push_back(cv[i]);
}
}

void charVector :: print()
{
for (int i=0; i<cv.size(); i++)
{
cout <>s1>>s2;
ch1.initializeVector(s1);
ch2.initializeVector(s2);
ch1.dupVector();
ch2.dupRevVector();
ch1.print();
cout<<endl;
ch2.print();
cout<<endl;
}

1
9

Reply
jayant nigam - March 30, 2020

guys the code is correct

3
1

Reply
ritika19 - March 31, 2020

the code is wrong because in dupVector() and dupRevVector(), the size of cv is changing. so you need to use a copy of the initial cv in the for loop.

Reply
Keerthan Reddy - April 2, 2020

The given code isn’t working.Please re-check the code

Reply
Aditya Aspat - April 9, 2020

Guys the code is working, if I’m not wrong you guys are copying the whole program. Just copy the functions and it’ll be done don’t copy the main and class

Reply
JAWAHAR - April 10, 2020

codes/mainc-5557-1586517717.568911.cpp:23:7: error: redefinition of ‘class charVector’
class charVector{
^~~~~~~~~~
codes/mainc-5557-1586517717.568911.cpp:5:7: note: previous definition of ‘class charVector’
class charVector
^~~~~~~~~~
codes/mainc-5557-1586517717.568911.cpp: In function ‘int main()’:
codes/mainc-5557-1586517717.568911.cpp:68:5: error: redefinition of ‘int main()’
int main()
^~~~
codes/mainc-5557-1586517717.568911.cpp:54:5: note: ‘int main()’ previously defined here
int main()
^~~~

Reply
Pratyush Kumar - April 10, 2020

5
2

Reply

Leave a Reply:

This site uses cookies and by using the site you are consenting to this.