data type string

21
Data Type string #include <string> // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout << “str1: ” << str1 << endl << “str2: ” << str2; str2 = str1; 1

Upload: jewell

Post on 06-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Data Type string. #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

TRANSCRIPT

Page 1: Data Type string

Data Type string

#include <string>// C++ String class

string str1, str2;// Default constructor

cin >> str1 >> str2;cout << “str1: ” << str1 << endl << “str2: ” << str2;

str2 = str1;

1

Page 2: Data Type string

C++ Class string

if (str1 == “CS2340”) cout << “Programming in VB!”;

if (str1 > str2) cout << “str1 appears after str2 in a dictionary.”;else if (str1 < str2) cout << “str1 appears before str2 in a dictionary.”;else cout << “str1 is the same as str2.”

str1 is less than str2 if word str1 is listed before str2 in a dictionary

2

Page 3: Data Type string

C++ Class string

cout << “str1 has “ << str1.length()

<< “ chars.”;

cout << “str2 has “ << str2.size() << “ chars.”;

// Just like cin.get() and cin.eof()// They are objects of some classes

3

Page 4: Data Type string

C++ Class string

String is implemented as an array of char

cin >> str1; cout << “The first char of str1 ” << str1[0];

cout << “The last char of str1 ” //<< str1[?]; << str1[str1.length() - 1];

// Change the first char of str1 to ‘A’.str1[0] = ‘A’;

4

Page 5: Data Type string

C++ Class string

SubString Function

cin >> str1; cout << “The first three chars of str1: ” << str1.substr(0, 3);// substring starting at index 0 // with length 3

cout << “The last three chars of str1: ” << str1.substr(str1.length() – 3, 3);

5

Page 6: Data Type string

C++ Class string

getline(cin, str1, ‘\n’);// not a member function of class string

int pos = str1.find(“ ”);// a member function of class string

str1[pos] = ‘_’;cout << str1;// Change the first space to ‘_’.

6

Page 7: Data Type string

C String

const int NAME_LENGTH = 15;

char LastName[NAME_LENGTH + 1]; // One more for the null char ‘\0’.

cout << "Enter last name: "; cin >> LastName;

// No loops needed!// C++ reads chars until White Characters, // then inserts a ‘\0’.// The array is treated as a string.// and ended with a null char ‘\0’.

7

Page 8: Data Type string

C String

cout << "The last name is " << LastName;

// C++ displays one char at a time

// until a '\0' is reached.

What if there is no null character?

8

Page 9: Data Type string

C String

char name1[16], name2[16];// Up to 15 chars!

cout << "Enter last name: "; cin >> name1;

name2 = name1;// Can we do this?

cin >> name2;if (name1 == name2) // Can we do this? cout << “Same name!”;

NO!9

Page 10: Data Type string

C String Functions

#include <cstring>//#include <string.h>

Three functions:int strlen(const char str[]);void strcpy(char dest[], const char src[]);int strcmp(const char str1[], const char str2[]);

10

Page 11: Data Type string

C String Functions#include <cstring>

char name1[16], name2[16];

cout << "Enter last name: "; cin >> name1;

name2 = name1; // Valid?// NO!strcpy(name2, name1); // Yes!

cout << “name1 has ” << strlen(name1) << “ chars.”;

11

Page 12: Data Type string

Function strcmp()The function compares two strings one char at a time, and stops the

first time the two strings have different chars or a null char is reached for both str1 and str2.

The function returns the difference of the chars at the stopping postion of the two strings.

Return value from strcmp(srt1, srt2) Result 0 str1 the same as str2 > 0 str1 is larger than str2 (later in a dictionary) < 0 str1 is smaller than str2 (earlier in a dictionary)

str1 str2 strcmp(srt1, srt2)“CS143” “CS143” ?“CS1430” “CS143” ?“CS143” “CS1430” ?“CS113” “CS143” ?“100” “99” ?

12

Page 13: Data Type string

C String Functions#include <cstring>

char name1[16], name2[16];

cin >> name1 >> name2;

int result = strcmp(name1, name2);

if (result == 0) cout << “Same string.”;else if (result < 0) cout << “name1 is smaller than name2.”;else cout << “name1 is larger than name2.”;

13

Page 14: Data Type string

Function strcpy()//-----------------------------------------------// The function has two parameters:// dest[], array of char,// src[], array of char.// The function copies src[] to dest[] and inserts// a null char at the end.// Parameter: (out, in)

//-----------------------------------------------

void strcpy(char dest[], const char src[])

{ for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i];

return;}

// Correct?

14

Page 15: Data Type string

Function strcpy()//-----------------------------------------------// The function has two parameters:// dest[], array of char,// src[], array of char.// The function copies src[] to dest[] and inserts// a null char at the end.// Parameter: (out, in)

//-----------------------------------------------

void strcpy(char dest[], const char src[])

{ for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i];

dest[i] = ‘\0’; // Copy the NULL character.

return;}// Correct? 15

Page 16: Data Type string

Function strcpy()//-----------------------------------------------// The function has two parameters:// dest[], array of char,// src[], array of char.// The function copies src[] to dest[] and inserts// a null char at the end.// Parameter: (out, in)

//-----------------------------------------------

void strcpy(char dest[], const char src[])

{ int i;

for (i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i];

dest[i] = ‘\0’;

return;} 16

Page 17: Data Type string

Function strcpy()//-----------------------------------------------// The function has two parameters:// dest[], array of char,// src[], array of char.// The function copies src[] to dest[] and inserts// a null char at the end.// Parameter: (out, in)

//-----------------------------------------------

void strcpy(char dest[], const char src[])

{ int i = 0;

while (src[i] != ‘\0’) { dest[i] = src[i]; i ++; }

dest[i] = ‘\0’;

return;}

17

Page 18: Data Type string

Function strlen()//---------------------------------------------// The function has one parameter:// str[], array of char.// The function finds and returns the length of // str[], excluding the null// char at the end.// Parameter: (in)//---------------------------------------------int strlen(const char str[]){ int size = 0;

while (str[size] != ‘\0’) size ++;

return size;} 18

Page 19: Data Type string

Function strcmp()//-------------------------------------------------------// The function has two parameters:// str1[], array of char, null terminated,// str2[], array of char, null terminated.// The function returns an integer: // 0 when str1 is the same as str2// positive when str1 > str2// negative when str1 < str2.// Parameter: (in, in)//------------------------------------------------------int strcmp(const char str1[], const char str2[]){ int i;

for (i = 0; str1[i] != ‘\0’ && str2[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]);

return (str1[i] - str2[i]);}

19

Page 20: Data Type string

Function strcmp()//-------------------------------------------------------// The function has two parameters:// str1[], array of char, null terminated,// str2[], array of char, null terminated.// The function returns an integer: // 0 when str1 is the same as str2// positive when str1 > str2// negative when str1 < str2.// Parameter: (in, in)//------------------------------------------------------int strcmp(const char str1[], const char str2[]){ int i; // Can we check just str1[i]? for (i = 0; str1[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]);

return (str1[i] - str2[i]);}

Very Good!20

Page 21: Data Type string

C++ String and C String

Use C++ String

For Prog5

Use C String

For Prog6

Use C String

For Prog5

21