Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson 10 -- John Cole1.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
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 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.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Lecture 17: Characters, Strings, and the string class Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
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.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
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.
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 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.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Chapter Characters, Strings, and the string class 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Fundamentals of Characters and Strings
Characters, C-Strings, and More About the string Class
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
C Stuff CS 2308.
10.1 Character Testing.
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Lecture 20 Strings
C Characters and Strings
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1

Character Testing require cctype header file CS1 Lesson John Cole2

Character Case Conversion 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 '!' CS1 Lesson John Cole3

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 '!' CS1 Lesson John Cole4

C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character String literal (string constant): sequence of characters enclosed in double quotes " " : "Hi there!" CS1 Lesson John Cole5 Hi the re! \0

C-Strings Array of char s can be used to define storage for string: const int SIZE = 20; char city[SIZE]; Leave room for NULL at end Can enter a value using cin or >> – Input is whitespace-terminated – No check to see if enough space For input containing whitespace, and to control amount of input, use cin.getline() CS1 Lesson John Cole6

C-String Library Functions Require the cstring header file Functions take one or more C-strings as arguments. Can use: –C-string name –pointer to C-string –literal string CS1 Lesson John Cole7

C-String Library Functions Functions: –strlen(str) : returns length of C-string str char city[SIZE] = "Missoula"; cout << strlen(city); // prints 8 –strcat(str1, str2) : appends str2 to the end of str1 char location[SIZE] = "Missoula, "; char state[3] = "MT"; strcat(location, state); // location now has "Missoula, MT" CS1 Lesson John Cole8

C-String Library Functions Functions: –strcpy(str1, str2) : copies str2 to str1 const int SIZE = 20; char fname[SIZE] = "Maureen", name[SIZE]; strcpy(name, fname); Note: strcat and strcpy perform no bounds checking to determine if there is enough space in receiving character array to hold the string it is being assigned. You’ll get compiler warnings in Visual Studio. CS1 Lesson John Cole9

Search Within a C-String Function: – strstr(str1, str2) : finds the first occurrence of str2 in str1. Returns a pointer to match, or NULL if no match. char river[] = "Wabash"; char word[] = "aba"; cout << strstr(river, word); // displays "abash" Why does it display “abash”? CS1 Lesson John Cole10

String/Numeric Conversion CS1 Lesson John Cole11 require cstdlib header file

String/Numeric Conversion int iNum; long lNum; double dNum; char intChar[10]; iNum = atoi("1234"); // puts 1234 in iNum lNum = atol("5678"); // puts 5678 in lNum dNum = atof("35.7"); // puts 35.7 in dNum itoa(iNum, intChar, 8); // puts the string // "2322" (base 8 for ) in intChar CS1 Lesson John Cole12

String/Numeric Conversion if C-string contains non-digits, results are undefined –function may return result up to non-digit –function may return 0 itoa does no bounds checking – make sure there is enough space to store the result. In Visual Studio you’ll get warnings. CS1 Lesson John Cole13

Writing C-String Functions Designing C-String Handling Functions –can pass arrays or pointers to char arrays –Can perform bounds checking to ensure enough space for results –Can anticipate unexpected user input CS1 Lesson John Cole14

Substring Function char * substr(const char *inStr, int start, int len) { char *sub = 0; if (!(start strlen(inStr))) { sub = new char[len + 1]; strncpy(sub, &inStr[start], len); sub[len] = '\0'; } return sub; } CS1 Lesson John Cole15

The C++ string Class Special data type supports working with strings #include 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; CS1 Lesson John Cole16

string Constructors string name; // Empty string string name(“John”); // Initializes string string next(strName); // Also initializes string sub(strName,2); // Takes first 2 chars CS1 Lesson John Cole17

string Comparison Can use relational operators directly to compare string objects: string str1 = "George", str2 = "Georgia"; if (str1 < str2) cout << str1 << " is less than " << str2; Comparison is performed similar to strcmp function. Result is true or false This is very different from Java, where you are comparing the references, not the strings. CS1 Lesson John Cole18

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" CS1 Lesson John Cole19

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 CS1 Lesson John Cole20

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" CS1 Lesson John Cole21