STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 1400 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
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.
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.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 Chapter 10 Characters, Strings, and the string class.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Lecture 17: Characters, Strings, and the string class Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CS102 Introduction to Computer Programming Chapter 10 Characters and Strings.
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.
Functions Why we use functions C library functions Creating our own functions.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
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.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 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.
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.
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.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
C Characters and Strings
Chapter 10 – Characters and Strings
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
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 Characters and Strings
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

STARTING OUT WITH STARTING OUT WITH Class 9 Honors

2 Objectives Use the string and character input/output functions Use the character type functions in ctype.h Understand how to use the string conversion functions to validate numeric input

3 Objectives Use the C++ string class Explain the difference between strings created and used as cstrings versus string

4 Review of Pointer/Arrays/Strings char color1[ ]=“blue”; char * colorptr=“blue”; char color2[5]={‘b’,’l’,’u’,’e’,’\0’}; sizeof color1 5 sizeof colorptr 2 sizeof color2 5 cout << color1; blue cout << colorptr; blue cout << color2; blue

5 Review of Pointer/Arrays/Strings char color1[ ]=“blue”; char * colorptr=“blue”; char color2[5]={‘b’,’l’,’u’,’e’,’\0’}; color1[2] *colorptr *(color2+3) colorptr[1] colorptr + 2 color2 u b e l &u &b

6 Review of Pointer/Arrays/Strings char buffer[8]; char * ptr; cin >> buffer; CORRECT INCORRECT cin >> ptr; Does buffer contain an address? Does ptr contain an address? Does buffer point to allocated memory for storing characters? Does ptr point to allocated memory for storing characters? YES NO YESNO

7 Character Testing char Letter = ‘a’; if(isupper (Letter)) cout << “Letter is uppercase\n”; else cout << “Letter is lower case.\n”; i<strlen (colorptr);i++) colorptr[i] = toupper(colorptr[i]); Table 10-1 P. 566 convert char * colorptr = “blue” to uppercase using toupper for( int i = 0;

8 Character Testing int main() { char Customer[8]; cout << “Enter a customer number in the form”; cout << “LLLNNNN\n”; cin.getline(Customer,8); // user enters abc1234 if (TestNum(Customer)) cout << “That’s valid”; else cout << “not the proper format”; return 0; } P. 568 Pgrm 10-2

9 Character Testing int TestNum(char CustNum[]) { for (int Count=0; Count < 3; Count++) { if (!isalpha(CustNum[Count])) return false; } for (Count =3; Count < 7; Count++) { if (!isdigit(CustNum[Count])) return false;} return true; } P. 568 Pgrm 10-2

10 Character Testing int TestNum(char CustNum[]) { for (int Count=0; Count < 3; Count++) { if (!isalpha(CustNum[Count])) return false; } for (Count =3; Count < 7; Count++) { if (!isdigit(CustNum[Count])) return false;} return true; } P. 568 Pgrm 10-2

11 Exercise on Validating Numeric Input Prompt user for a number cout << “Enter a number\n”; Get response into a character array Check each character entered to see if it is a digit char buffer[6]; cin >> buffer; for (int i=0; i < strlen (buffer); i++) if(!isdigit(buffer[i])) { flag = 1; break; }

12 Exercise on Validating Numeric Input do { flag =0; cout << “Enter a number\n”; cin >> buffer; for (int i=0; i < strlen (buffer); i + +) if ( !isdigit (buffer[i]) ) { flag = 1; cout << “You have entered” << “an invalid number”; break;} } while (flag = = 1); int flag; char buffer[6];

13 atoiaccepts a string and converts the string to an integer and returns that value atofaccepts a string and converts the string to a float and returns than value int number; number = atoi(buffer); itoaConverts an integer to a string

14 C-String Functions from cstring Table 10-3 strlenaccepts a pointer to a string as an argument and returns the number of characters in the string excluding the null strcat accepts two pointers to two strings and appends the contents of the second string to the first string, altering the first string P.579

15 C-String Functions from cstring Table 10-3 strcpyaccepts two pointers to two strings as arguments and copies the second string on top of the first string, replacing the first string; the null is copied onto the end of the first string

16 String Functions from cstring Table 10-3 strncpy accepts two pointers to two strings and an integer as arguments and copies a specific number of characters from the second string on top of the first string, replacing the first string; the null is NOT copied onto the end of the first string; it is the programmers responsibility to put it there

17 String Functions from cstring Table 10-3 strstr accepts two pointers to two strings as arguments and searches for the first occurrence of the second string in the first string; a pointer to the first character of the substring is returned; if none is found, a NULL pointer is returned

18 String Copy cout << “enter your last name”; cin >> last; strcpy (name, last); name first last char name[13]; char last [6];char first [5]; // user enters JONES J JOES ES \0 N ON???????

19 String Concatenation name first last J JOES ES \0 N ON??????? cout << name; cout << last; Displays JONES cout << “enter your first name ”; // user enters TOMcin >> first; TOM\0

20 String Concatenation name first last J JOES ES \0 N ON??????? TOM strcat (name, ”,”);

21 String Concatenation name first last J JOES ES \0N ON? ????? TOM, strcat (name, ”,”); strcat (name,first);

22 String Concatenation name first last JOES\0N TOM strcat (name,first); JESON, strcat (name, ”,”); OM\0T??? ?

23 Searching for a substring inside a string char Array[ ] = “Four score and seven years ago”; char *StrPtr; StrPtr = strstr(Array,”seven”); cout << StrPtr; // displays seven years ago

24 “string” data type string movieTitle; // #include char bookTitle[20]; // #include movieTitle = “Wheels of Fury”; // or cin >> movieTitle; bookTitle = “From Here to Eternity”; // or cin >> bookTitle; strcpy(bookTitle,”From Here to Eternity”); cout << movieTitle << bookTitle; P

25 “string” data type string name1; // #include string name2; cout << “Enter a name (last name first):”; getline(cin,name1); cout << “Enter another name:”; getline(cin,name2); cout << “Here are the names sorted” << “alphabetically”; if (name1 < name2) cout << name1 << name2; else cout << name2 << name1; P.609 Prgm 10-15

26 “string” data type operations not available with cstring type character arrays +=Appends a copy of the string on the right to the string object on the left +Returns a string that is the concatenation of the two string operands []Implements array-subscript notation Relational Operators = == != P.611 Table 10-9

27 “string” data type operations not available with cstring type character arrays string str1, str2, str3; str1 = “ABC”; str2 = “DEF”; str3 = str1 + str2; cout << str1; // displays ABC cout << str2; // displays DEF cout << str3; // displays ABCDEF str3 += “GHI”; cout << str3; // displays ABCDEFGHI P.611 Pgrm 10-17

28 “string” data type operations string theString = “This is the time…”; cout << theString.at(3); theString.clear(); theString = “for all good men”; cout << theString.find(theString,’o’); cout << theString.length(); String result = theString.substr(4,3); cout << result; P.614 Table s 1 16 all

29 Q & A

STARTING OUT WITH STARTING OUT WITH Conclusion of Class 9