Manipulating strings

JancypriyaM 898 views 8 slides Feb 15, 2019
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

Manipulating string, Creating objects, manipulating string objects, Relational operations, String characteristics, Accessing characteristics in string, comparing and swapping


Slide Content

M.Jancypriya
Assistant Professor
Department of computer Applications
Bon Secours College for Women,
Thanjavur

Sequence of characters. Built-in functions
are not supported by C++.
Using string class,

#include<string>

Creating string objects
Reading string objects from keyboard
Displaying string objects to the screen
Finding a substring from a string
Modifying string
Adding objects of string
Comparing strings
Accessing characters of a string
Obtaining the size or length of a string

String s1; // no args
String s2(“xyz”); // one arg
S1=s2 // assigning obj
S3=“abc”+s2 // concatenating string
Cin>>s1; // reading a word through
keyboard
S3+=s1; // overloading +op s3=s3+s1

strcpy(str1, str2): Copies string str2 into string str1.

strcat(str1, str2): Concatenates string str2 onto the
end of string str1.

strlen(str1): Returns the length of string str1.

strcmp(str1, str2): Returns 0 if str1 and str2 are the
same; less than 0 if str1<str2; greater than 0 if
str1>str2.

strstr(str1, str2): Returns a pointer to the first
occurrence of string str2 in string str1

append(): This function appends a part of
a string to another string.

erase(): This function removes character
as specified.

Insert() : insert a character at specified
location.

Replace()- replace the specific character
with a given string.

void relational_operation(string s1, string s2)
{
 string s3 = s1 + s2;

 if(s1 != s2)
 cout << s1 << " is not equal to " << s2 << endl;

 if(s1 > s2)
 cout << s1 << " is greater than " << s2 << endl;

 else if(s1 < s2)
 cout << s1 << " is smaller than " << s2 << endl;

 if(s3 == s1 + s2)
 cout << s3 << " is equal to " << s1 + s2 << endl;

}

// Main function
int main()
{
string s1("Geeks");
string s2("forGeeks");
relational_operation(s1, s2);

return 0;
}

Output:
Geeks is not equal to forGeeks
Geeks is smaller than forGeeks
GeeksforGeeks is equal to GeeksforGeeks