String and Character Manipulation.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
CS 1400 Pick ups from chapters 2 and 3. #include directive This pre-processing directive causes the textual contents of a named file to be inserted into.
CS 1400 Feb 2007 Pick ups from chapters 2 and 3. Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
1 Chapter 10 Characters, Strings, and the string class.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Characters, Strings, And The string Class Chapter 10.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC 270 – Survey of Programming Languages
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Chapter Characters, Strings, and the string class 10.
Program Input and the Software Design Process ROBERT REAVES.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Strings.
Topic 2 Input/Output.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Characters, C-Strings, and More About the string Class
getline() function with companion ignore()
Standard Version of Starting Out with C++, 4th Edition
Input and Output Chapter 3.
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Null-Terminated Character Arrays
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Counting Loops.
10.1 Character Testing.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 3: Expressions and Interactivity
Standard Version of Starting Out with C++, 4th Edition
Using string type variables
character manipulation
getline() function with companion ignore()
Odds and Ends.
Presentation transcript:

String and Character Manipulation

C-style Strings (ntcas) char name[10] = “Clayton”; #include using std::string;... string name = “Clayton”; C++ Standard Library Strings (a.k.a. Standard String Class)

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

7

std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;

std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl; User enters: Pattie Boyd

std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl; System outputs: Pattie

std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

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?

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

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

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

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?

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

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

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;

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;

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;

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;

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.

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?

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

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?

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

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

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

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;

std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

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?

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

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

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

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?

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

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

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

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;

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]

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]

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]

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 Chimley\0?? [0][1][2][3][4][5][6][7][8][9]

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 Chimley\0?? [0][1][2][3][4][5][6][7][8][9]

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.

#include  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

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;

2

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;

1

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;

HELLO! HI.

Write Your Own bool IsDigit (const char input)

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

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

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

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

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

Input Options  getline()  reads line-by-line  cin >>  reads word-by-word  get()  reads character-by-character

End of Session