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.