Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Programming Fundamentals

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

2 resize(int n) This function is used to resize / shrink / reduce the


characters in a string to first n characters

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

5 append(string str) This function is to append / extend a string to another


string

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

Implementation of string operations:


1. size():

#include <iostream>
#include <string>
using namespace std;

void main()
{
string name = "IIUI";
string name2 = "Islamabad";

// displays size of string


cout << "Size of " << name << " is "<< name.size() << endl;
cout << "Size of " << name2 << " is "<< name2.size() << endl;
system("pause");
}

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");
}

3. swap (string str):


#include <iostream>
#include <string>
using namespace std;

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;

// swapping fruit1 with fruit 2


fruit1.swap(fruit2);
cout << "\n";

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";

// concatenation two string with additional space


string twoFruits = fruit1 + " " + fruit2;
cout << "I have two fruits that are: " << twoFruits << endl;

system("pause");
}

5. append(string str):

#include <iostream>
#include <string>
using namespace std;

void main()
{
string fruit1 = "apple";
string fruit2 = "mango";

string twoFruits = fruit1;


// appending a space to “apple”
twoFruits.append(" ");
// appending a “mango” to “apple ”
twoFruits.append(fruit2);

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 << "Enter your full name: ";


// getting input using getline
getline(cin, str2);

cout << "Enter your full name again: ";


// getting input using cin
cin >> str1;

cout << "Name entered using getline: " << str2 << endl;
cout << "Name entered using cin: " << str1 << endl;

system("pause");
}

7. insert (position, str)


#include <iostream>
#include <string>
using namespace std;

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>

using namespace std;


void main()
{

string str1="There are two needles in this haystack with needles.";


string str2="needle";
string str3="nothing";
int foundAtPosition;
foundAtPosition=str1.find(str2);
//will give first position where needle is found in str1
cout<<"SEARCH RESULT OF needle"<<endl<<foundAtPosition<<endl;
foundAtPosition=str1.find(str3);
//will give first position where nothing is found in str1
cout<<"SEARCH RESULT OF nothing"<<endl<<foundAtPosition<<endl;

system("pause");

}
10. substr(position,length)
#include<iostream>
#include <string>
using namespace std;
void main()
{

string str1="We think in generalities, but we live in details.";


string str2;
str2=str1.substr(3,5);
//substring str2 created, contains 5 characters from str1 starting at
position 3
cout<<"Substring of "<<str1<<"is "<<endl<<str2<<endl;

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)

You might also like