String Data Type and Its Functions
String Data Type and Its Functions
String Operations
String is a collection of characters surrounded by double quotes. They are used for
storing text in C++. You need to add #include <string> at top of program to user
strings
Declaration and initialization of String:
#include <iostream>
#include <string> // this is necessary to use string in program
using namespace std;
void main()
{
string name = "IIUI";
cout << name << endl;
system("pause");
}
Output:
STRING BUILT IN FUNCTIONS
There are several operations we can do with strings in our program. Some of them
are as follows:
1 size() This function is used to find count of characters in a string
3 swap(string str) This function is used to swap a string with another string
str
4 + ‘+’ sign when applied to two variable with data type int,
double, float or long adds their values. When working
with strings, the values of both strings are concatenated
6 getline(cin, str) When takinga string as input using cin , the value entered
by user is accepted till the first occurance of SPACE or
ENTER. Using this function, the string input is considered
as the value entered by user till first occurance of ENTER
7. insert(int position, Inserts the string str just before the position mentioned
string str) in the function
8. replace(startingposition, Replaces the portion of the string that begins at starting
charCount, str) position and replaces charCount characters by new
contents given in str
9. find(str) Finds str in the given string and returns the first position
when str is found. Incase, str not found, this returns a
value of -1
10. substr(position,length) Generates a substring which is the portion of the string
that starts at character position and has length specified
#include <iostream>
#include <string>
using namespace std;
void main()
{
string name = "IIUI";
string name2 = "Islamabad";
2. resize(n):
#include <iostream>
#include <string>
using namespace std;
void main()
{
string university = "International Islamic University Islamabad";
// resize the string to only first 13 characters i.e. International
university.resize(13);
cout << university << endl;
system("pause");
}
void main()
{
string fruit1 = "apple";
string fruit2 = "mango";
cout << "Value of fruit 1 before swapping: " << fruit1 << endl;
cout << "Value of fruit 2 before swapping: " << fruit2 << endl;
cout << "Value of fruit 1 after swapping: " << fruit1 << endl;
cout << "Value of fruit 2 after swapping: " << fruit2 << endl;
system("pause");
}
4. +:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string fruit1 = "apple";
string fruit2 = "mango";
system("pause");
}
5. append(string str):
#include <iostream>
#include <string>
using namespace std;
void main()
{
string fruit1 = "apple";
string fruit2 = "mango";
cout << "I have two fruits that are: " << twoFruits << endl;
system("pause");
}
6. getline(cin, str):
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str1;
string str2;
cout << "Name entered using getline: " << str2 << endl;
cout << "Name entered using cin: " << str1 << endl;
system("pause");
}
void main()
{
string str1="to be question";
string str2="the ";
cout<<"BEFORE INSERT"<<str1<<endl;
str1.insert(6,str2); // will insert str2 just before 6th position
cout<<"AFTER INSERT"<<str1<<endl;
system("pause");
}
8. replace(startingposition, charCount, str)
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str1="this is a test string.";
string str2="n example";
cout<<"BEFORE REPLACE"<<endl<<str1<<endl;
str1.replace(9,5,str2);
// starting from position 9 this will replace 5 characters with str2
cout<<"AFTER REPLACE"<<endl<<str1<<endl;
system(“pause”);
}
9. find(str)
#include<iostream>
#include <string>
system("pause");
}
10. substr(position,length)
#include<iostream>
#include <string>
using namespace std;
void main()
{
system("pause");
}
PRACTICE TASKS
1. Write a program that prompts user to enter two strings and check if those
string are equal in size or not.
2. Initialize str1 and str2 with your first and last name respectively. Then display
your complete name
a. By using +
b. By using append
3. Input your complete registration number. Then find CS, SE or IT in your
registration number. If CS is found display Computer Scientist, if SE is found
display Software Engineer and if IT is found, display IT Engineer on screen.
4. Initialize str1 with “cup” and str2 with “cake”. Then insert str2 in str1 such that
the result is “cupcake”.
5. When a student transfers from one degree program to another, her
registration number remains same except for the degree program initials. e.g.
if 1234-FBAS-BSCS-F19 transfers to software engineering degree program her
new registration number would be 1234-FBAS-BSSE-F19. Write a C++ program
that asks the student her original registration number and her new degree
program initials. Then your program displays her new registration number.
(Include CS, SE and IT)