See discussions, stats, and author profiles for this publication at: [Link]
net/publication/348960185
Strings in C++
Presentation · February 2021
DOI: 10.13140/RG.2.2.34523.69923
CITATIONS READS
0 3,719
1 author:
Tarfa Hamed
University of Mosul
38 PUBLICATIONS 283 CITATIONS
SEE PROFILE
All content following this page was uploaded by Tarfa Hamed on 02 February 2021.
The user has requested enhancement of the downloaded file.
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Dealing with Strings in C++
• A string is an ordered sequence of characters, enclosed
in double quotation marks. It is part of the Standard Library.
• You need to include the <string> library to use the string data
type. Alternatively, you can use a library that includes
the string library.
#include <string>
using namespace std;
int main() {
string a = "I am learning C++";
return 0;
}
• The <string> library is included in the <iostream> library, so you
don't need to include <string> separately, if you already
use <iostream>.
The C-Style Character String
• The C-style character string originated within the C language and
continues to be supported within C++.
• This string is actually a one-dimensional array of characters
which is terminated by a null character '\0'.
• The following declaration and initialization create a string
consisting of the word "Hello".
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
1
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• To hold the null character at the end of the array, the size of the
character array containing the string is one more than the number
of characters in the word "Hello."
• If you follow the rule of array initialization, then you can write
the above statement as follows:
char greeting[] = "Hello";
• Following is the memory presentation of above defined string in
C/C++:
• Actually, you do not place the null character at the end of a string
constant.
• The C++ compiler automatically places the '\0' at the end of the
string when it initializes the array. Let us try to print above-
mentioned string:
#include <iostream>
using namespace std;
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
2
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• 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:
No. Function & Purpose
strcpy(s1, s2);
1.
Copies string s2 into string s1.
strcat(s1, s2);
2.
Concatenates string s2 onto the end of string s1.
strlen(s1);
3.
Returns the length of string s1.
strcmp(s1, s2);
4. Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
strchr(s1, ch);
5.
Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2);
6.
Returns a pointer to the first occurrence of string s2 in string s1.
• Following example makes use of few of the above-mentioned
functions:
#include <iostream>
#include <cstring>
using namespace std;
3
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
int main () {
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << str1 << endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout << "length of str1 = " << len << endl;
return 0;
}
• When the above code is compiled and executed, it produces
result something as follows:
Hello
HelloWorld
length of str1 = 10
The String Class in C++
• The standard C++ library provides a string class type that
supports all the operations mentioned above, additionally much
more functionality.
• Let us check the following example:
4
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
#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 << endl;
// concatenates str1 and str2
str3 = str1 + str2;
cout << str3 << endl;
// total length of str3 after concatenation
len = [Link]();
cout << len << endl;
return 0;
}
The output of the above program will be the same as the previous
program.
Exercises:
1- Write a C++ program to reverse a given string. Example: if the input
string was “Hello” then the output string should be “olleH”.
5
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
2- Write a C++ function called find() that searches for a certain
character in a string. If the character is found, the function returns
the index of that character in the string. Otherwise, it returns -1.
Example 1: find(“Computer”,’p’) the output will be 3.
Example 2: find(“Computer”,’w’), the output will be -1.
3- Write a C++ program to swap a certain character in a string.
Example:
string s1 = “Computer”
If we want to swap the letter ‘r’ in s1 with the letter ‘s’ then the output
should be “Computes”
4- Write a C++ function called substr() that returns a substring from a
string.
Example:
string s1 = “Science”;
if we call the above function substr(s1, 2,6), then the output should be
“ience”
5- Write a C++ function called beginswith() that checks if a given
string starts with a given character.
Example 1: beginswith(“Programming”,’r’) the output will be false
Example 2: beginswith(“Programming”,’P’) the output will be true
6- Write a C++ function called endswith() that checks if a given string
ends with a given character.
6
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Example 1: endswith(“Computer”,’y’) the output will be false
Example 2: endswith(“Computer”,’r’) the output will be true
7- Write a C++ function called count() that counts how many a
specific character is repeated in a given string.
Example 1: count(“statistics”, ‘s’) the output will be 3
Example 2: count(“statistics”, ‘n’) the output will be 0
8- Write a C++ program to remove any repeated character in a string.
Example: if the input string was “samsung” then the output should
be “samung”
9- Write a C++ program that prints all the repeated characters in a
string. Example: if the input string was “Motorola”, then the output
string should be “o”
10- Write a C++ function called uppcase() that returns the uppercase
of a given string. Example: if the input string was “program”, then
the output string should be “PROGRAM”.
View publication stats