February 14, 2005 Characters, Strings and the String Class.

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

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
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.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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.
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.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
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.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 1400 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char.
CS 1400 Apr 18, 2007 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool.
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.
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.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1 Chapter 10 Characters, Strings, and the string class.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
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
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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.
Characters and Strings
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.
Characters and Strings
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
C Characters and Strings
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Arrays in C.
Chapter 8 - Characters and Strings
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
10.1 Character Testing.
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Lecture 20 Strings
C Characters and Strings
Presentation transcript:

February 14, 2005 Characters, Strings and the String Class

The char datatype char is usually a one byte integer datatype To get the ascii code for a char, you can do this static_cast ( ‘a’) To get the char corresponding to an integer (ascii code) static_cast ( 97)

Two Char Functions char intToChar( int n ) { return static_cast (n); } char charToInt( char c ) { return static_cast (c); }

Automatic Conversion Since char is an integer data type, the + operator will automatically cast the answer to an int. sizeof(‘A’) = 1 and sizeof(‘A’+’A’)=4

charToInt simplified Since + casts a character to an int, we can simply add 0 to a char to get its numerical ascii equivalent. ‘A’+0 equals 65

Characters really are integers in C++ cout << (‘A’==65) << endl; prints out true. (which is normally 1 in C++)

But Remember Characters that represent numbers are not equal as numbers to the numbers they represent cout << ‘1’+0 << endl; Prints 49, not 1. What does 1 represent? Lets see.

Some char functions isalpha – Is a character a letter of the alphabet isalnum – Is a character a letter of the alphabet or a digit isdigit – Is a character a digit islower – Is a character lower case isupper – Is a character upper case isprint - is a character a printable character ispunct – is a character a printable character other than a digit, a letter or a space isspace – is a character a whitespace character. Whitespace includes any of the following: space ‘ ‘, newline ‘\n’, vertical tab ‘\v’, tab ‘\t’,

Finding a digit in a string char code[] = “hgdhdgfk3hagsdfkhgasdfh7ahsgdfkahsgdfhk"; char *tmp=code; while (*tmp != '\0') { if (isdigit(*tmp)) { cout << *tmp << endl; } tmp++; }

C-Strings in C++ A C-string is an array of characters where the final character is ‘\0’ which equals NULL which equals 0. These three characters all have ascii value 0. A ‘string literal’ is a literal representation of a string in a program. In C++, string literals are enclosed in double quotes. The terminating ‘\0’ is automatically appended

cin and strings Suppose you declare a string like this: char firstName[10]; And then use cin to allow the user to type in their first name like this. cin >> firstName Since firstName is just a pointer to the memory location of the first letter, cin has no way of knowing how big the string is. It will assign letters to any number of locations. If a name has more than ten letters, cin will go beyond the ‘\0’ of firstName and continue stuffing letters in memory locations that are not under strict control of the program. What to do?

cin.getline(firstName, 10) The function getline is made exactly for this purpose. The second argument specifies the maximum length including the null terminator.

C-String Library Functions strlen strcat char first[12] = "Jack"; char last[8] = " Widman"; strcat(first, last); cout << first << endl; strcpy strncpy strcmp strstr

String/Numeric Conversions atoi atoi(“7”) atol atof itoa - cout << itoa(1256, number, 16) << endl;

The C++ String Class Much legacy code (that is, code that is already written and you are modifying or debugging or extending it ) uses C-strings. That is why we are learning them. For new code, however, the C++ String class is vastly superior.

#include - need this header Simple code example string firstName; cout << “What is your first name?” << endl; cin >> firstName; cout << “Welcome “ << firstName << endl;

An easier way to concatenate string first = “Sue”; string last = “Jones”; cout << first + “ “ + last << endl;

Array Access You can still do this: string companyName=“Microsoft”; cout << companyName[0] << endl; will print out ‘M’;

This Wednesday More about the String class