0% found this document useful (0 votes)
6 views4 pages

Document

This C++ code demonstrates various functions available in the String class, including string creation, input, length calculation, character access, appending, concatenation, comparison, substring extraction, searching, and modification. It also shows how to convert a C++ string to a C-style string. The code contains several examples illustrating each function's usage.

Uploaded by

keshavpatel2287
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Document

This C++ code demonstrates various functions available in the String class, including string creation, input, length calculation, character access, appending, concatenation, comparison, substring extraction, searching, and modification. It also shows how to convert a C++ string to a C-style string. The code contains several examples illustrating each function's usage.

Uploaded by

keshavpatel2287
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

// C++ Code to demostrate various functions available in

// String class

#include <iostream>

#include <string>

Using namespace std;

Int main()

// Creating and initializing strings

String greeting = “Hello, World!”;

Cout << greeting << endl;

String name;

// Input from the user

Cout << “Enter your name: “;

Cin >> name;

Cout << name << endl;

// String length

Int length = greeting.length();

Cout << length << endl;

// Accessing characters

Char firstChar = greeting[0];


Char secondChar = greeting.at(1);

Cout << firstChar << “ “ << secondChar << endl;

// Appending and concatenating strings

String firstName = “Geek”;

String lastName = “Geeks”;

String fullName = firstName + “ “ + lastName;

Cout << fullName << endl;

String base = “Hello”;

Cout << base << endl;

Base.append(“ World!”);

Cout << base << endl;

// String comparison

String str1 = “apple”;

String str2 = “banana”;

If (str1 == str2) {

Cout << “Strings are equal” << endl;

Else {

Cout << “Strings are not equal” << endl;

Int result = str1.compare(str2);

If (result == 0) {

Cout << “Strings are equal” << endl;


}

Else if (result < 0) {

Cout << “str1 comes before str2” << endl;

Else {

Cout << “str1 comes after str2” << endl;

// Substrings

String text = “Hello, World!”;

Cout << text << endl;

String sub = text.substr(7, 5);

Cout << sub << endl;

// Searching

String searchIn = “C++ Programming”;

Size_t position = searchIn.find(“Programming”);

If (position != string::npos) {

Cout << “Found at position “ << position << endl;

Else {

Cout << “Not found” << endl;

// Modifying strings

String modify = “I like dogs.”;


Modify.replace(7, 4, “cats”);

Cout << modify << endl;

Modify.insert(6, “ black”);

Cout << modify << endl;

Modify.erase(6, 6);

Cout << modify << endl;

// Conversion

String str = “C++”;

Const char* cstr = str.c_str();

Cout << cstr << endl;

Return 0;

You might also like