0% found this document useful (0 votes)
9 views9 pages

Chapter 3 (Strings)

This document covers the concept of strings in C++, including their declaration, initialization, and various operations such as concatenation, comparison, and copying. It provides examples of string manipulation techniques and emphasizes the importance of understanding the string data type and its functionalities. Learning outcomes include the ability to manipulate and format strings effectively in programming.

Uploaded by

mayurg0909
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)
9 views9 pages

Chapter 3 (Strings)

This document covers the concept of strings in C++, including their declaration, initialization, and various operations such as concatenation, comparison, and copying. It provides examples of string manipulation techniques and emphasizes the importance of understanding the string data type and its functionalities. Learning outcomes include the ability to manipulate and format strings effectively in programming.

Uploaded by

mayurg0909
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/ 9

DIT1123- Object Oriented Programming with C++

CHAPTER 3
TOPIC: Strings

Kukutla Alekhya
Objectives

Understand the concept of strings as a data type for representing


and manipulating textual data.
Declare and initialize string variables using appropriate syntax.
Demonstrate knowledge of string literals and escape sequences to
represent special characters within strings.
Strings

string class that represents sequence of characters. We can perform


many operations on strings such as concatenation, comparison,
conversion etc.

#include <iostream>
using namespace std;
int main( ) { Output:
string s1 = "Hello"; Hello
char ch[] = { 'C', '+', '+'}; C++
string s2 = string(ch);
cout<<s1<<endl;
cout<<s2<<endl;
}
String Compare Example

#include <iostream>
#include <iostream>
#include <cstring> Output:
using namespace std; What is my favourite fruit? apple
int main () What is my favourite fruit? banana
{ What is my favourite fruit? mango
char a[] = "mango"; Answer is correct!!
char b[50];
do {
cout<<"What is my favourite fruit? ";
cin>>b;
} while (strcmp (a,b) != 0);
cout<<"Answer is correct!!"<<endl;
return 0;
}
String Concat Example

#include <cstring>
using namespace std;
int main()
{
char a[25], b[25];
cout << "Enter the first string: ";
cin>>a; Output:
cout << "Enter the second string: "; Enter the key string: Welcome to
cin>>b; Enter the buffer string: C++ Programming.
strcat(a, b); Key = Welcome to C++ Programming.
cout << "a = " << a << endl; Buffer = C++ Programming.
cout << "b = " << b<<endl;
return 0;
}
String Copy Example

String Copy Example


#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char a[25], b[25]; Output:
cout << "Enter the string: "; Enter the string: ISBAT
cin>>a; String= ISBAT
strcpy(b, a); copy string = ISBAT
cout << "string = "<< a << endl;
cout << "copy string = "<< b<<endl;
return 0;
}
String Length Example

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ary[] = "Welcome to C++ Programming";
cout << "Length of String = " << strlen(ary)<<endl;
return 0;
}

Length of String = 26
Learning outcomes

Knowledge of String Data Type: Students will demonstrate an understanding of


the string data type, including its purpose, characteristics, and representation in
the programming language being used.
String Manipulation: Students will be able to manipulate strings by performing
operations such as concatenation, splitting, and extracting substrings. They will
understand the available methods and functions for string manipulation and
apply them appropriately.
String Formatting: Students will demonstrate the ability to format strings for
output by incorporating variables, expressions, and formatting options. They will
understand the syntax and use of string formatting techniques, such as using
placeholders or format specifiers.
Thank you

You might also like