Object Oriented Programming: Chapter 5: Templates and Exception Handling

String

C++ Object Oriented Programming

String is a collection of characters. In C++ we can write strings in two ways 1) C‒style Strings 2) C++ string class

String

•  String is a collection of characters. In C++ we can write strings in two ways –


1) C‒style Strings

The C‒style strings can be represented as arrays of characters ending with null character '\0'.

C++ Program

#include<iostream>

using namespace std;

int main() {

     char s1[] = "Hello";

     char s2[] = "Friends";

     cout<<"First String is: "<<s1<<endl;

     cout<<"Second String is: "<<s2<<endl;

     return 0;

}

Output

First String is: Hello

Second String is: Friends


2) C++ string class

The C++ string class is provided by Standard Template Library(STL). This class is defined in the header <string>

C++ program

#include<iostream>

#include<cstring>

using namespace std;

int main() {

      string s1 = "Hello";

      string s2 = "Friends";

      cout<<"First String is: "<<s1<<<endl;

      cout<<"Second String is: "<<s2<<endl;

      cout<<"Concatenated String is: "<<s1+s2<<endl;

      cout<<"Length of First string is: "<<s1.length()<<endl;

      return 0;

}

Output

First String is: Hello

Second String is: Friends

Concatenated String is: HelloFriends

Length of First string is: 5

 

Object Oriented Programming: Chapter 5: Templates and Exception Handling : Tag: Oops, Computer Programming : C++ Object Oriented Programming - String


Object Oriented Programming: Chapter 5: Templates and Exception Handling



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation