SlideShare a Scribd company logo
1 of 18
By: Dr. Dawlat Mustafa Sulaiman
Duhok Polytechnic University
Technical College of Administration/First Year
Data Structures
Lecture
C++ Strings:
One of the most useful data types supplied in the
C++ libraries is the string. A string is a type of object
(variable) that stores a sequence of letters or other
characters, such as "Hello" or "May 10th is my
birthday!". Just like the other data types.
The string class stores the characters of a
string as a collection of bytes in contiguous
memory locations. Strings are most commonly
used in the programs where string). When we
need to work with texts. We can perform
various operations on the strings in C++.
Used C++ string header <string>
and namespace std at top of
program
#include <string>
using namespace std;
C++ provides following two types of
string representations
 1- The C-style character string.
 2- The string class type introduced with
Standard C++.
1.The C-Style Character String
a. 1- This string is actually a one-
dimensional array of characters which is
terminated by a null character '0‘
b. 2- The following declaration and initialization
create a string consisting of the word "Hello".
char greeting[6] = {'H', 'e', 'l', 'l', 'o','0'};
char greeting[] = "Hello";
Alternative ways of defining a
string
char str[4] = "C++";
char str[] = {'C','+','+','0'};
char str[4] = {'C','+','+','0'};
Like arrays, it is not necessary to use all the space allocated for the string. For
example:
char str[100] = "C++";
memory presentation of defined string
in C++
The C++ compiler automatically places the '0' at
the end of the string when it initializes the array.
Let us try to print “Hello”
Example
#include <iostream>
using namespace std;
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it produces
the following result −
Greeting message: Hello
C++ supports a wide range of functions that
manipulate null-terminated strings
Sr.No Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the
end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less
than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of
character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of
string s2 in string s1.
Example
#include <iostream>
#include <string>
using namespace std;
int main ()
{ char str1[10] = "Hello";
char str2[10] = "World";
char str3[10]; int len ;
strcpy( str3, str1); // copy str1 into str3
cout << "strcpy( str3, str1) : " << str3 << endl;
strcat( str1, str2); // concatenates str1 and str2
cout << "strcat( str1, str2): " << str1 << endl;
len = strlen(str1); // total lenghth of str1 after
concatenation
cout << "strlen(str1) : " << len << endl;
return 0; }
Output
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
Example 1:
(write a program to display a string entered by the
user)
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin >> str;
cout << "You entered: " << str << endl;
cout << "nEnter another string: ";
cin >> str;
cout << "You entered: "<<str<<endl;
return 0;
}
Output
Enter a string: C++
You entered: C++
Enter another string:
Programming is fun.
You entered: Programming
Example 2:
C++ program to read and display an entire line of
text entered by the user.
#include<iostream>
using namespace std;
int main()
{char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
cout << "You entered: " << str << endl;
return 0;
}
Output
Enter a string: Programming is
fun.
You entered: Programming is
fun.
2- The String Class Type in
C++
 The standard C++ library provides a string class
type that supports all the operations mentioned
above, additionally much more functionality.
 In C++, you can also create a string object
for holding strings.
 Unlike using char arrays, string objects
has no fixed length, and can be extended as per
your requirement
#include <iostream>
#include <string>
using namespace std;
int main () {
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
// copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// concatenates str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// total length of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
Output
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
Example 3:
C++ string using the string data type
#include<iostream>
#include<string>
using namespace std;
int main()
{
// Declaring a string object
string str;
cout<<"Enter a string: ";
getline(cin, str);
cout<<"You entered: "<<str<<endl;
return 0;
}
In this program, a string str is declared.
Then the string is asked from the user.
Instead of using cin>> or cin.get() function, you can
get the entered line of text using getline().
getline() function takes the input stream as the first
parameter which is cin and str as the location of
the line to be stored.
THANKS

More Related Content

Similar to lecture-5 string.pptx

16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
Abdul Samee
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 

Similar to lecture-5 string.pptx (20)

Strings
StringsStrings
Strings
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
C++ Strings.ppt
C++ Strings.pptC++ Strings.ppt
C++ Strings.ppt
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
ppt8-string-1.pptx
ppt8-string-1.pptxppt8-string-1.pptx
ppt8-string-1.pptx
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
 
String in c programming
String in c programmingString in c programming
String in c programming
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
Unit 2
Unit 2Unit 2
Unit 2
 
Programming in c plus plus4
Programming in c plus plus4Programming in c plus plus4
Programming in c plus plus4
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 

Recently uploaded

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

lecture-5 string.pptx

  • 1. By: Dr. Dawlat Mustafa Sulaiman Duhok Polytechnic University Technical College of Administration/First Year Data Structures Lecture
  • 2. C++ Strings: One of the most useful data types supplied in the C++ libraries is the string. A string is a type of object (variable) that stores a sequence of letters or other characters, such as "Hello" or "May 10th is my birthday!". Just like the other data types. The string class stores the characters of a string as a collection of bytes in contiguous memory locations. Strings are most commonly used in the programs where string). When we need to work with texts. We can perform various operations on the strings in C++.
  • 3. Used C++ string header <string> and namespace std at top of program #include <string> using namespace std;
  • 4. C++ provides following two types of string representations  1- The C-style character string.  2- The string class type introduced with Standard C++.
  • 5. 1.The C-Style Character String a. 1- This string is actually a one- dimensional array of characters which is terminated by a null character '0‘ b. 2- The following declaration and initialization create a string consisting of the word "Hello". char greeting[6] = {'H', 'e', 'l', 'l', 'o','0'}; char greeting[] = "Hello";
  • 6. Alternative ways of defining a string char str[4] = "C++"; char str[] = {'C','+','+','0'}; char str[4] = {'C','+','+','0'}; Like arrays, it is not necessary to use all the space allocated for the string. For example: char str[100] = "C++";
  • 7. memory presentation of defined string in C++ The C++ compiler automatically places the '0' at the end of the string when it initializes the array. Let us try to print “Hello”
  • 8. Example #include <iostream> using namespace std; int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; cout << "Greeting message: "; cout << greeting << endl; return 0; } When the above code is compiled and executed, it produces the following result − Greeting message: Hello
  • 9. C++ supports a wide range of functions that manipulate null-terminated strings Sr.No Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1.
  • 10. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. 5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1. 6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
  • 11. Example #include <iostream> #include <string> using namespace std; int main () { char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ; strcpy( str3, str1); // copy str1 into str3 cout << "strcpy( str3, str1) : " << str3 << endl; strcat( str1, str2); // concatenates str1 and str2 cout << "strcat( str1, str2): " << str1 << endl; len = strlen(str1); // total lenghth of str1 after concatenation cout << "strlen(str1) : " << len << endl; return 0; } Output strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10
  • 12. Example 1: (write a program to display a string entered by the user) #include <iostream> using namespace std; int main() { char str[100]; cout << "Enter a string: "; cin >> str; cout << "You entered: " << str << endl; cout << "nEnter another string: "; cin >> str; cout << "You entered: "<<str<<endl; return 0; } Output Enter a string: C++ You entered: C++ Enter another string: Programming is fun. You entered: Programming
  • 13. Example 2: C++ program to read and display an entire line of text entered by the user. #include<iostream> using namespace std; int main() {char str[100]; cout << "Enter a string: "; cin.get(str, 100); cout << "You entered: " << str << endl; return 0; } Output Enter a string: Programming is fun. You entered: Programming is fun.
  • 14. 2- The String Class Type in C++  The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality.  In C++, you can also create a string object for holding strings.  Unlike using char arrays, string objects has no fixed length, and can be extended as per your requirement
  • 15. #include <iostream> #include <string> using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout << "str3 : " << str3 << endl; // concatenates str1 and str2 str3 = str1 + str2; cout << "str1 + str2 : " << str3 << endl; // total length of str3 after concatenation len = str3.size(); cout << "str3.size() : " << len << endl; return 0; } Output str3 : Hello str1 + str2 : HelloWorld str3.size() : 10
  • 16. Example 3: C++ string using the string data type #include<iostream> #include<string> using namespace std; int main() { // Declaring a string object string str; cout<<"Enter a string: "; getline(cin, str); cout<<"You entered: "<<str<<endl; return 0; }
  • 17. In this program, a string str is declared. Then the string is asked from the user. Instead of using cin>> or cin.get() function, you can get the entered line of text using getline(). getline() function takes the input stream as the first parameter which is cin and str as the location of the line to be stored.