String and Character Manipulation http://cs.mst.edu C-style Strings - - PowerPoint PPT Presentation

string and character manipulation
SMART_READER_LITE
LIVE PREVIEW

String and Character Manipulation http://cs.mst.edu C-style Strings - - PowerPoint PPT Presentation

String and Character Manipulation http://cs.mst.edu C-style Strings (ntcas) char name[10] = Clayton; C++ Standard Library Strings (a.k.a. Standard String Class) #include <string> using std::string; ... string name =


slide-1
SLIDE 1

http://cs.mst.edu

String and Character Manipulation

slide-2
SLIDE 2

http://cs.mst.edu

C-style Strings (ntcas)

char name[10] = “Clayton”; #include <string> using std::string; ... string name = “Clayton”;

C++ Standard Library Strings

(a.k.a. Standard String Class)

slide-3
SLIDE 3

http://cs.mst.edu

std::string

#include <string> using std::string; int main() { string name = “Clayton”; cout << name.length();

slide-4
SLIDE 4

http://cs.mst.edu

slide-5
SLIDE 5

http://cs.mst.edu

std::string input limitations

string name; cout << “What‟s your name? ”; cin >> name; cout << name << endl;

slide-6
SLIDE 6

http://cs.mst.edu

std::string input limitations

string name; cout << “What‟s your name? ”; cin >> name; cout << name << endl;

User enters: Pattie Boyd

slide-7
SLIDE 7

http://cs.mst.edu

std::string input limitations

string name; cout << “What‟s your name? ”; cin >> name; cout << name << endl;

System outputs: Pattie

slide-8
SLIDE 8

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

slide-9
SLIDE 9

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

System outputs: What’s your name?

slide-10
SLIDE 10

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

User enters: James Marshall

slide-11
SLIDE 11

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

System places “James Marshall” into the input buffer

slide-12
SLIDE 12

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

System pulls “James” from the buffer and into name

slide-13
SLIDE 13

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

System outputs: What is your hometown?

slide-14
SLIDE 14

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

System does not wait for the user input

slide-15
SLIDE 15

http://cs.mst.edu

std::string input limitations

string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

Instead “Marshall” is pulled from the buffer and into hometown

slide-16
SLIDE 16

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

slide-17
SLIDE 17

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

slide-18
SLIDE 18

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

slide-19
SLIDE 19

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

slide-20
SLIDE 20

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

Delimiter Character is NOT included in input; it’s discarded.

slide-21
SLIDE 21

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

System outputs: What’s your name?

slide-22
SLIDE 22

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

User enters: Janis Joplin

slide-23
SLIDE 23

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

System outputs: What is your hometown?

slide-24
SLIDE 24

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

User enters: Port Arthur

slide-25
SLIDE 25

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

System outputs: Janis Joplin

slide-26
SLIDE 26

http://cs.mst.edu

std::string input fix

// getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What‟s your name? ”; getline(cin, name, „\n‟); cout << “What is your hometown?”; getline(cin, hometown, „\n‟); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

System continues output: Janis JoplinPort Arthur

slide-27
SLIDE 27

http://cs.mst.edu

std::string alternative fix

string name; string hometown; cout << “What‟s your name? ”; getline(cin, name); cout << “What is your hometown?”; getline(cin, hometown); cout << “Name: “ << name; cout << “Hometown: “ << hometown;

slide-28
SLIDE 28

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

slide-29
SLIDE 29

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

System outputs: What’s your age?

slide-30
SLIDE 30

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

User enters: 8

slide-31
SLIDE 31

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

System places “8\n” into the input buffer

slide-32
SLIDE 32

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

System pulls “8” from the buffer and into age

slide-33
SLIDE 33

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

System outputs: What’s your name?

slide-34
SLIDE 34

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

System does not wait for the user input

slide-35
SLIDE 35

http://cs.mst.edu

std::string more input nuances

string name; int age; cout << “What‟s your age? ”; cin >> age; cout << “What‟s your name? ”; getline(cin, name);

Instead it pulls “\n” from the buffer into name and getline is finished

slide-36
SLIDE 36

http://cs.mst.edu

std::string vs C-string(ntca)

string sname; const int SIZE = 80; char cname[SIZE]; cout << “What‟s your given name? ”; getline(cin, sname); cout << “What‟s your family name? ”; cin.getline(cname, SIZE-1); //different syntax!!

slide-37
SLIDE 37

http://cs.mst.edu

more C-string getline

const int SIZE = 10; char name[SIZE]; cout << “What‟s your name? ”; cin.ignore(500, „\n‟); cin.getline(name, SIZE-1); cout << name << endl;

slide-38
SLIDE 38

http://cs.mst.edu

more C-string getline

const int SIZE = 10; char name[SIZE]; cout << “What‟s your name? ”; cin.ignore(500, „\n‟); cin.getline(name, SIZE-1); cout << name << endl;

? ? ? ? ? ? ? ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

slide-39
SLIDE 39

http://cs.mst.edu

more C-string getline

const int SIZE = 10; char name[SIZE]; cout << “What‟s your name? ”; cin.ignore(500, „\n‟); cin.getline(name, SIZE-1); cout << name << endl;

System outputs: What’s your name?

? ? ? ? ? ? ? ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

slide-40
SLIDE 40

http://cs.mst.edu

more C-string getline

const int SIZE = 10; char name[SIZE]; cout << “What‟s your name? ”; cin.ignore(500, „\n‟); cin.getline(name, SIZE-1); cout << name << endl;

System clears any extraneous \n from the input buffer

? ? ? ? ? ? ? ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

slide-41
SLIDE 41

http://cs.mst.edu

more C-string getline

const int SIZE = 10; char name[SIZE]; cout << “What‟s your name? ”; cin.ignore(500, „\n‟); cin.getline(name, SIZE-1); cout << name << endl;

User enters: Chimley

C h i m l e y \0 ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

slide-42
SLIDE 42

http://cs.mst.edu

more C-string getline

const int SIZE = 10; char name[SIZE]; cout << “What‟s your name? ”; cin.ignore(500, „\n‟); cin.getline(name, SIZE-1); cout << name << endl;

System outputs: Chimley

C h i m l e y \0 ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

slide-43
SLIDE 43

http://cs.mst.edu

Extraction vs. getline

  • The extraction operator (cin>>variable) will

always skip leading whitespace and leave \n in the buffer.

  • The getline functions will read leading

whitespace and will not leave the \n in the buffer. Rule: always know what is in your input buffer.

slide-44
SLIDE 44

http://cs.mst.edu

#include<cctype>

  • toupper(char)
  • returns the uppercase of arg sent toupper('a'); -> 'A„
  • tolower(char)
  • similar
  • isupper(char)
  • returns bool: true if uppercase isupper('a'); -> false
  • islower(char)
  • similar
  • isalpha(char)
  • similar
  • isdigit(char)
  • similar
  • ispunct(char)
  • returns bool: true if punctuation ispunct('!'); -> true
  • isspace(char)
  • returns bool: true if whitespace – space, newline, tab
slide-45
SLIDE 45

http://cs.mst.edu

C-string Manipulation

int i = 0, count = 0; char ntca[20] = “Hello! Hi.”; while (ntca[i] != „\0‟) { if (ispunct(ntca[i])) count++; i++; } cout<<count<<endl;

slide-46
SLIDE 46

http://cs.mst.edu

slide-47
SLIDE 47

http://cs.mst.edu

C-string Manipulation

int i = 0, count = 0; char ntca[20] = “Hello! Hi.”; while (ntca[i] != „\0‟) { if (isspace(ntca[i])) count++; i++; } cout<<count<<endl;

slide-48
SLIDE 48

http://cs.mst.edu

slide-49
SLIDE 49

http://cs.mst.edu

C-string Manipulation

int i = 0, count = 0; char ntca[20] = “Hello! Hi.”; while (ntca[i] != „\0‟) { ntca[i] = toupper(ntca[i]); i++; } cout<<ntca<<endl;

slide-50
SLIDE 50

http://cs.mst.edu

HELLO! HI.

slide-51
SLIDE 51

http://cs.mst.edu

Write Your Own

bool IsDigit (const char input)

slide-52
SLIDE 52

http://cs.mst.edu

Write Your Own

bool IsDigit (const char input) { bool digit = false; if (input >= 48 && input <= 57) digit = true; return digit; }

slide-53
SLIDE 53

http://cs.mst.edu

Write Your Own

bool IsDigit (const char input) { return(input>=48 && input<=57); }

slide-54
SLIDE 54

http://cs.mst.edu

Character Input and Output

  • get – allows one character to be read from input
  • cin.get(char_variable);
  • peek – reads the next character from the input buffer

without extracting it

  • char_variable = cin.peek();
  • putback – places the character back into the input buffer
  • cin.putback(char_variable);
  • put – outputs a single character
  • cout.put(char_variable)
  • equivalent to cout << char_variable
slide-55
SLIDE 55

http://cs.mst.edu

get

char next; cout<<”enter your poem: “; do { cin.get(next); cout<<next; } while (next != „\n‟);

slide-56
SLIDE 56

http://cs.mst.edu

get

char next; cout<<”enter your poem: “; do { cin.get(next); cout<<next; } while (next != „\n‟); char poetry[500]; cout<<”enter your poem: “; cin.getline(poetry, 499); cout<<poetry;

getline vs.

slide-57
SLIDE 57

http://cs.mst.edu

Input Options

  • getline()
  • reads line-by-line
  • cin >>
  • reads word-by-word
  • get()
  • reads character-by-character
slide-58
SLIDE 58

http://cs.mst.edu

End of Session