Write a program which contains a class named String (please take care of the case). The class should be capable of holding a string containing more than one word.?
-
The class should at least have a parametric constructors by which we are going to supply a sentence at run time. Class string should be defined in this way that the following statement should be executed with their proper output. String str1; //string with length 0 String str2; cin>>str2; // to print the char elements of str2; (Donât use any string library function for the any purpose) You are free to add any C++ concepts, any other data members, member functions and constructors you think are necessary to solve the problem.
-
Answer:
#include<iostream> #define MAXLEN 2000 using namespace std; class String{ char *s; unsigned int length; public: String(){ s=new char[MAXLEN]; length=0; } unsigned int len(){ return length; } friend ostream& operator<< (ostream &out,String ob){ out<<ob.s; return out; } friend istream& operator>> (istream &in,String &ob){ in.get(ob.s,MAXLEN); for(int i=0;ob.s[i]!='\0';i++) ob.length++; in.get(); return in; } }; int main(){ String a; cin>>a; cout<<"You Entered: "<<a; cout<<"\nLength: "<<a.len(); } Output: Hello World You Entered: Hello World Length: 11
Jignesh Jain at Quora Visit the source
Related Q & A:
- Is it correct to extend a bean class and write a class which holds logic to populate the bean we extend?Best solution by stackoverflow.com
- How to write a program to monitor the temperature of CPU?Best solution by Stack Overflow
- Can I record a program/movie from a cable dvr to a DVD recorder?Best solution by Yahoo! Answers
- Is it better to get a higher grade in a easier class or a mediocre/low grade in a hard class?Best solution by greatcollegeadvice.com
- How to write a persuasive letter to a bank?Best solution by wikihow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.