Question

C++ program which make use of  six different string class constructors, and six different string class methods.

C++ program which make use of  six different string class constructors, and six different string class methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// below is c++ code

#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str1="shivraj kumar"; // first constructor initialize name
string str2(str1); // this is parametrized constructor
// here value of str1 is copies in str2 variable
string str3(str1,7,5); //extract letters from index 7 to length 5 from string str1 and save in str3;
string str4(str2,8); // extract length of 8 character from str2 and add it in str4
string str5(5,'x'); // create a 5 length x
string str6(str2.begin(),str2.begin()+6); //create a 6 length variable from str2 and add it in str6

cout<<"first constructor example "<<str1<<endl;
cout<<"second constructor example "<<str2<<endl;
cout<<"third constructor example "<<str3<<endl;
cout<<"fourth constructor example "<<str4<<endl;
cout<<"fifth constructor example "<<str5<<endl;
cout<<"sixth constructor example "<<str6<<endl;

cout<<"first method example of computing string length "<<str1.length()<<endl;
cout<<"second method example of computing starting character of str1 "<<str1.at(0)<<endl;
cout<<"third method example of computing and finding string shiva in str1 "<<str1.find("shiva")<<endl;
cout<<"fourth method example of computing and appending last name in str1 "<<str1.append(" pandit")<<endl;
cout<<"fifth method example of computing substring of length 3 from index 0 in str1 "<<str1.substr(0,3)<<endl;
str1.clear();
cout<<"sixth method example of computing and clearing str1 so length of str1 is "<<str1.length();


return 0;
}

//output

kumar C:\Users\Shivraj kumar\Desktop\che\jhdjweg.exe first constructor example shivraj kumar second constructor example shi

Add a comment
Know the answer?
Add Answer to:
C++ program which make use of  six different string class constructors, and six different string class methods.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++, program that use the built in string class, its constructors, some of its methods and...

    C++, program that use the built in string class, its constructors, some of its methods and operators. It reads an input file of English text, one word at a time. The file should consist of about 500 words. Keep only the words, removing any punctuation or other characters. .Store the words in a built-in STL container, such as vector or map.,Choose a list of about 10 words, for example, {she,, and, he, or, the}., Remove these words from the list.,,...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable...

    Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable string. This class represents a character array and provide functionalities need to do basic string processing. It contains an array of characters, representing a textual string. Overview In Java, Strings are a great device for storing arrays of characters. One limitation of Strings in Java, however (there is always at least one), is that they are immutable (ie. they cannot be changed once created)....

  • how to write a class definition: member variables, methods, constructors

    how to write a class definition: member variables, methods, constructors

  • /*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, //...

    /*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, // helper methods, and the this operator. // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments //-------------------------------------------------------------------------*/ import java.util.Scanner; public class { public static void main(String[] args) { // Let's make two students using all two constructors // Write code to create a new student alice using constructor #1 //--> Student alice = // Write code to...

  • create a Person class with two fields (name(String), age(int)) create all the required methods such as...

    create a Person class with two fields (name(String), age(int)) create all the required methods such as the accessors, mutators, toString(),constructors etc. Use the comparable interface and implement the compareTo method such that the person objects can be compared and sorted according to their age.

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • 1) Introduction to Objects (with Constructors and Properties) We need to answer the question which are...

    1) Introduction to Objects (with Constructors and Properties) We need to answer the question which are below. and instruction are given down starting with part(b). Just we need to copy paste code file and follow the instruction and answer related to them. a) Enter the following program.Answer The questions below the codes, Notice that it consists of two classes that will go into the same project. Please put each class into its own code file (use Lab5_1 for the Project...

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

  • Write a program in C++ that reads a string consisting of a positive integer or a...

    Write a program in C++ that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. DO NOT USE STACK LIBRARY OR STACK FUNCTIONS OR A STRING LIBRARY. For this assignment you have to use STACK with all methods to convert the decimal number to numeric...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT