10.1 Character Testing.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
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.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Strings.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
CS161 Introduction to Computer Science Character Data.
CS Nov 2006 C-strings.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
C-strings and C++ string Class
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1 Chapter 10 Characters, Strings, and the string class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
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.
Characters, Strings, And The string Class Chapter 10.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC 270 – Survey of Programming Languages
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter Characters, Strings, and the string class 10.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Characters, Strings, and the cstring Library
C-Strings We have already seen that a C-string is a null-terminated array of characters.
C Characters and Strings
Characters, Strings, and the cstring Library
String Processing Upsorn Praphamontripong CS 1110
String String Builder.
Characters, C-Strings, and More About the string Class
Chapter 8 - Characters and Strings
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
Chapter 4: Making Decisions
Chapter 7: Strings and Characters
C++ STRINGS string is part of the Standard C++ Library
Strings A collection of characters taken as a set:
Chapter 8 More on Strings and Special Methods
String class and its objects
CS 1111 Introduction to Programming Spring 2019
Standard Version of Starting Out with C++, 4th Edition
Std Library of C++.
Today’s Objectives 28-Jun-2006 Announcements
C Characters and Strings
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

10.1 Character Testing

Character Testing require cctype header file FUNCTION MEANING isalpha true if arg. is a letter, false otherwise isalnum true if arg. is a letter or digit, false otherwise isdigit true if arg. is a digit 0-9, false otherwise islower true if arg. is lowercase letter, false otherwise isprint true if arg. is a printable character, false otherwise ispunct true if arg. is a punctuation character, false otherwise isupper true if arg. is an uppercase letter, false otherwise isspace true if arg. is a whitespace character, false otherwise

From Program 10-1

Character Case Conversion Require cctype header file Functions: toupper: if char argument is lowercase letter, return uppercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << toupper(ch1); // displays 'H' cout << toupper(ch2); // displays 'E' cout << toupper(ch3); // displays '!'

Character Case Conversion Functions: tolower: if char argument is uppercase letter, return lowercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << tolower(ch1); // displays 'h' cout << tolower(ch2); // displays 'e' cout << tolower(ch3); // displays '!'

10.7 The C++ string Class

The C++ string Class Special data type supports working with strings #include <string> Can define string variables in programs: string firstName, lastName; Can receive values with assignment operator: firstName = "George"; lastName = "Washington"; Can be displayed via cout cout << firstName << " " << lastName;

Input into a string Object Use cin >> to read an item into a string: string firstName; cout << "Enter your first name: "; cin >> firstName;

Input into a string Object Use getline function to put a line of input, possibly including spaces, into a string: string address; cout << "Enter your address: "; getline(cin,address);

string Comparison Can use relational operators to compare string objects: string str1 = "George”, str2 = "Georgia"; if (str1 < str2) cout << str1 << " is less than ” << str2; Comparison similar to strcmp function. Result is true if equal or false otherwise. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

Other Definitions of C++ strings Meaning string name; defines an empty string object string myname("Chris"); defines a string and initializes it string yourname(myname); string aname(myname, 3); defines a string and initializes it with first 3 characters of myname string verb(myname,3,2); defines a string and initializes it with 2 characters from myname starting at position 3 string noname('A', 5); defines string and initializes it to 5 'A's

string Operators OPERATOR MEANING >> extracts characters from stream up to whitespace, insert into string << inserts string into stream = assigns string on right to string object on left += appends string on right to end of contents on left + concatenates two strings [] references character in string using array notation >, >=, <, <=, ==, != relational operators for string comparison. Return true or false

string Operators string word1, phrase; string word2 = " Dog"; cin >> word1; // user enters "Hot Tamale" // word1 has "Hot" phrase = word1 + word2; // phrase has // "Hot Dog" phrase += " on a bun"; for (int i = 0; i < 16; i++) cout << phrase[i]; // displays // "Hot Dog on a bun"

string Member Functions Are behind many overloaded operators Categories: assignment: assign, copy, data modification: append, clear, erase, insert, replace, swap space management: capacity, empty, length, resize, size substrings: find, substr comparison: compare See Table 10-7 for a list of functions

string Member Functions string word1, word2, phrase; cin >> word1; // word1 is "Hot" word2.assign(" Dog"); phrase.append(word1); phrase.append(word2); // phrase has "Hot Dog" phrase.append(" with mustard relish", 13); // phrase has "Hot Dog with mustard" phrase.insert(8, "on a bun "); cout << phrase << endl; // displays // "Hot Dog on a bun with mustard"