Вы находитесь на странице: 1из 12

W07

String Manipulation

Introduction

 A string is a sequence of characters.

 C-style strings: Null terminated char arrays

 ANSI C++ provides a class called string.

 #include <string>

W6&7-Operator Overloading
3

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 1
Characters in C
 char is a data type capable of holding a
character

 Character constants
 'a', 'b', 'c', …'z', '0', '1', … '9', '+', '-', '=',
'!', '~', etc, '\n', '\t', '\0', etc.
 A-Z, a-z, 0-9 are in order, so that arithmetic can be done

for (char a = ‘A’; a <= ‘Z’; a++)


cout << a << ‘\t’;

W6&7-Operator Overloading

Strings in C

 A string is a character array ending in '\0’


 char s[256];

 String constants are in double quotes


 char t[] = "This is an initialized string!";
 char *u = "This is another string!";

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 2
Strings in C
 Let
 char *u = "This is another string!";
 Then
 u[0] == 'T'
u[1] == 'h'
u[2] == 'i'

u[21] == 'g'
u[22] == '!'
u[23] == '\0'

W6&7-Operator Overloading

Character Functions in C
 <ctype.h>

 These return or false (0) or true (non-zero)


int isdigit(int c) int isalpha(int c)
int isalnum(int c) int isxdigit(int c)
int islower(int c) int isupper(int c)
int isspace(int c) int iscntrl(int c)

 These change case (if appropriate)


int toupper(int c)
int tolower(int c)

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 3
Support for Strings in C

 Most string manipulation is done through functions in


<string>

 int strlen(char *s) – returns length of string


 Excluding final '\0‘

 char* strcpy(char *s, char *ct) – Copies


string ct to string s, returns s
 s must be big enough to hold contents of ct

W6&7-Operator Overloading

Support for Strings in C

 int strcmp(char *s, char *t)


 compares s and t, returns <0 if s < t,

 >0 if s > t, zero if s and t are identical

 char* strcat(char *s, char *ct)


 Concatenates string ct to the end of string s,
returns s
 s must be big enough to hold contents of both
strings!

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 4
String Operations in C++

 Creating string objects.


 Reading string objects from keyboard.
 Displaying string objects to the screen.
 Finding a substring from a string.
 Modifying string objects.
 Adding string objects.
 Accessing characters in a string.
 Obtaining the size of string.
 And many more…….

W6&7-Operator Overloading

String Constructors

 string();
 For creating an empty string.
 string(const char *str);
 For creating a string object from a null-
terminated string.
 string(const string &str);
 For creating a string object from other string
object.

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 5
Creating String Objects
 string s1, s3; // Using constructor with no arguments.
 string s2(“xyz”); // Using one-argument constructor.
 s1 = s2; // Assigning string objects
 s3 = “abc” + s2; // Concatenating strings

 cin >> s1; // Reading from keyboard (one word)


 cout << s2; // Display the content of s2
 getline(cin, s1);// Reading from keyboard a line of text

 s3 += s1; // s3 = s3 + s1;
 s3 += “abc”; // s3 = s3 + “abc”;

W6&7-Operator Overloading

Manipulating String Objects


 insert(pos, str): Inserts additional characters before the
character indicated by pos.

 erase(pos, len): Erases the portion of the string value that


begins at the character position pos and spans len characters (or
until the end of the string)

string s1(“12345”);
string s2(“abcde”);
s1.insert(4, s2); // s1 = 1234abcde5
s1.erase(4, 5); // s1 = 12345

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 6
String Comparison

string s1(“ABC”);
string s2(“XYZ”);
int x = s1.compare(s2);

 x == 0 if s1 == s2
 x > 0 if s1 > s2
 x < 0 if s1 < s2

W6&7-Operator Overloading

Comparing and Swapping

 Overloaded version of compare


int compare(int start1, int length1, string s2, int start2, int length2)

string s1(“1234”), s2(“abcdef”);


int x = s1.compare(0, 2, s2, 2, 2);

s1.swap(s2)
 Exchanges the content of string s1 and s2

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 7
String Characteristics

Function Task
size() Number of elements currently stored
length() Number of elements currently stored
max_size() Maximum size of a string object that a system can
support
empty() Return true or 1 if the string is empty otherwise
returns false or 0
resize() Used to resize a string object (effects only size and
length)

W6&7-Operator Overloading

String Characteristics

void display (string &str)


{
cout << “Length = ” << str.length() << endl;
cout << “Max Size = ” << str.max_size() << endl;
cout << “Empty: ” << (str.empty() ? “yes” : “no”) << endl;
}

W6&7-Operator Overloading

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 8
Accessing Characters in Strings

Function Task
at() For accessing individual characters
substr() For retrieving a substring
find() For finding a specific substring
find_first_of() For finding the location of first occurrence of the specific
character(s)
find_last_of() For finding the location of last occurrence of the specific
character(s)

[] operator For accessing individual character. Makes the string


object to look like an array.

W6&7-Operator Overloading

String Manipulation Example

int main()
{
string s1("Quick! Send for Count Graystone.");
string s2("Lord");
string s3("Don’t ");
s1.erase(0, 7); //remove “Quick! “
s1.replace(9, 5, s2); //replace “Count” with “Lord”
s1.replace(0, 1, "s"); //replace ‘S’ with ‘s’
s1.insert(0, s3); //insert “Don’t “ at beginning

W6&7-Operator Overloading
19

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 9
String Manipulation Example
s1.erase(s1.size()-1, 1); //remove ‘.’
s1.append(3, '!'); //append “!!!”
int x = s1.find(' '); //find a space
while( x < s1.size() ) //loop while spaces remain
{s1.replace(x, 1, "/"); //replace with slash
x = s1.find(' '); } //find next space
cout << "s1: " << s1 << endl;
system("pause");
return 0;
}

W6&7-Operator Overloading
20

Using Array of strings

int main()
{
const int DAYS = 7; //number of strings in array
const int MAX = 10; //maximum size of each string
//array of strings
char star[DAYS][MAX]={“Sunday”, “Monday”,
“Tuesday”,“Wednesday”, “Thursday”,“Friday”, “Saturday” };

for (int j=0; j<DAYS; j++) //display every string


cout << star[j] << endl;
return 0;
}

W6&7-Operator Overloading
21

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 10
Using string as a Class Member
class part
{
private:
char partname[30]; //name of widget part
int partnumber; //ID number of widget part
double cost; //cost of part
public:
void setPart(char pname[], int pn, double c)
{
strcpy(partname, pname);
partnumber = pn;
cost = c;
}

W6&7-Operator Overloading
22

Using string as a Class Member

void showPart() //display data


{
cout << "\nName=" << partname;
cout << ", number=" << partnumber;
cout << ", cost=$" << cost;
}
};

W6&7-Operator Overloading
23

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 11
Using string as a Class Member
int main()
{
part part1, part2;
part1.setpart("handle bolt", 4473, 217.55);
part2.setpart("start lever", 9924, 419.25);
cout << "\nFirst part: "; part1.showpart();
cout << "\nSecond part: "; part2.showpart();
cout << endl;
system("pause");
return 0;
}

W6&7-Operator Overloading
24

Object-Oriented Programming-Spring 2019


Instructor: Saima Jawad 12

Вам также может понравиться