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.

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.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 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.
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:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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 Fundamentals of Strings and Characters Characters.
5.1Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
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.
February 14, 2005 Characters, Strings and the String Class.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
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.
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:
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 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. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Characters and Strings
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
Object Oriented Programming COP3330 / CGS5409
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
String in C++.
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Lecture 2 Arrays & Pointers September 7, 2004
C Characters and Strings
Presentation transcript:

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. Review on Thursday. Functions (overloading, pass-by-value, pass-by-reference) Recursion Arrays Pointers Sorting (insertion sort, selection sort, and bubble sort)

Today Recursion from Homework 4 –Recursive Adder Basic String Processing Recursion and Arrays –Palindrome –Recursive Insertion Sort

Basic String Processing Strings (arrays of chars) are very common They are usually referred to as char*s (remember a pointer is really the same as an array) …but be careful. Declaring an array allocates memory to store the data in, declaring a pointer just allocates memory for one address.

Fundamentals of Characters and Strings Character constant –Integer value represented as character in single quotes –'z' is integer value of z 122 in ASCII

Fundamentals of Characters and Strings String - series of characters treated as single unit Can include letters, digits, +, -, *, etc. String literal (string constants) Enclosed in double quotes, for example:"I like C++“ Array of characters, ends with null character '\0' String constant is a const pointer that points to string’s first character Like arrays

Fundamentals of Characters and Strings String assignment –Character array char color[] = "blue"; –Creates 5 element char array color last element is '\0' –Variable of type char * char *colorPtr = "blue"; –Creates pointer colorPtr to letter b in string “blue” »“blue” somewhere in memory –Alternative for character array char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘\0’ };

Fundamentals of Characters and Strings Reading strings –Assign input to character array word[20] cin >> word Reads characters until whitespace or EOF String could exceed array size cin >> setw( 20 ) >> word; Reads 19 characters (space reserved for '\0' )

String Manipulation Functions of the String-handling Library String handling library provides functions to –Manipulate string data –Compare strings –Search strings for characters and other strings –Tokenize strings (separate strings into logical pieces)

char *strcpy( char *s1, const char *s2 ); Copies the string s2 into the character array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ); Copies at most n characters of the string s2 into the character array s1. The value of s1 is returned. char *strcat( char *s1, const char *s2 ); Appends the string s2 to the string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. char *strncat( char *s1, const char *s2, size_t n ); Appends at most n characters of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively.

int strncmp( const char *s1, const char *s2, size_t n ); Compares up to n characters of the string s1 with the string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. char *strtok( char *s1, const char *s2 ); A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text— delimited by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current to­ ken is returned by each call. If there are no more tokens when the function is called, NULL is returned. int strlen( const char *s ); Determines the length of string s. The number of characters preceding the terminating null character is returned.

Copying strings –char *strcpy( char *s1, const char *s2 ) Copies second argument into first argument –First argument must be large enough to store string and terminating null character –char *strncpy( char *s1, const char *s2, size_t n ) Specifies number of characters to be copied from string into array Does not necessarily copy terminating null character String Manipulation Functions of the String-handling Library

String Manipulation Functions Concatenating strings –char *strcat( char *s1, const char *s2 ) Appends second argument to first argument First character of second argument replaces null character terminating first argument Ensure first argument large enough to store concatenated result and null character –char *strncat( char *s1, const char *s2, int n ) Appends specified number of characters from second argument to first argument Appends terminating null character to result

Comparing strings –Characters represented as numeric codes –Strings compared using numeric codes –Character codes / character sets ASCII –“American Standard Code for Information Interchange” String Manipulation Functions

Comparing strings –int strcmp( const char *s1, const char *s2 ) Compares character by character Returns –Zero if strings equal –Negative value if first string less than second string –Positive value if first string greater than second string –int strncmp( const char *s1, const char *s2, int n ) Compares up to specified number of characters Stops comparing if reaches null character in one of arguments String Manipulation Functions

Tokenizing –Breaking strings into tokens, separated by delimiting characters –Tokens usually logical units, such as words (separated by spaces) –"This is my string" has 4 word tokens (separated by spaces) String Manipulation Functions

–char *strtok( char *s1, const char *s2 ) Multiple calls required –First call contains two arguments, string to be tokenized and string containing delimiting characters »Finds next delimiting character and replaces with null character –Subsequent calls continue tokenizing »Call with first argument NULL Returns NULL if no characters matching the delimiter could be found String Manipulation Functions

/* strtok example */ #include int main () { char str[] ="This is a sample string, just testing."; char * pch; cout << "Splitting string " << str << “into tokens: ”; pch = strtok (str," "); while (pch != NULL) { cout << pch; pch = strtok (NULL, ",."); } return 0; } String Manipulation Functions

Splitting string "This is a sample string, just testing." in tokens: This is a sample string just testing String Manipulation Functions

Determining string lengths –int strlen( const char *s ) Returns number of characters in string –Terminating null character not included in length

String Manipulation Functions Write iterative palindrome Write recursive palindrome Write recursive insertion sort