cs 1430: programming in c++ 1. data type string #include // c++ string class string str1, str2; //...

23
CS 1430: Programming in C++ 1

Upload: paula-lesley-griffith

Post on 06-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

C++ Class string if (str1 == “CS2340”) cout str2) cout

TRANSCRIPT

Page 1: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

CS 1430: Programming in C++

1

Page 2: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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

string str1, str2;// Default constructor

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

str2 = str1;

2

Page 3: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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

(ASCII code ordering)

3

Page 4: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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

4

Page 5: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

C++ Class stringString 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’;

5

Page 6: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

C++ Class stringSubString 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);

6

Page 7: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

C++ Class stringgetline(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 ‘_’.

7

Page 8: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

C Stringconst 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’.

8

Page 9: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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?

9

Page 10: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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!10

Page 11: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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[]);

11

Page 12: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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.”;

12

Page 13: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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” ?

13

Page 14: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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.”;

14

Page 15: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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: ( ? , ? )// Parameter: (out, in)//-----------------------------------------------void strcpy(char dest[], const char src[]){ for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i];

return;}

// Correct?

15

Page 16: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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?

16

Page 17: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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

17

Page 18: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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

18

Page 19: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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: ( ? )// Parameter: ( IN )//---------------------------------------------int strlen(const char str[]){ int size = 0;

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

return size;}

19

Page 20: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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: ( ? , ? )// 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]);}

20

Page 21: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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’ && str2[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]);

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

21

Page 22: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

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; // Just check 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!22

Page 23: CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout

Schedule• Prog5

Due Time: 10 PM, Thursday

• Lab10Due 5 PM, Thursday

• Friday?

23