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

Slides:



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

EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
 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.
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.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
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.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Lecture 12 Pointers and Strings Section 5.4, ,
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)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Characters and Strings
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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,
 2006 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
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.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
C Characters and Strings
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
Characters and Strings
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
String in C++.
Chapter 5 - Pointers and Strings
5.1 Introduction Pointers Powerful, but difficult to master
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Lecture 2 Arrays & Pointers September 7, 2004
Characters and Strings Functions
Characters and Strings
C Characters and Strings
Presentation transcript:

EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

Fundamentals of Characters and Strings  Character constant –Integer value represented as character in single quotes – 'z' is integer value of z 122 in ASCII  String –Series of characters treated as single unit –Can include letters, digits, special characters +, -, *... –String literal (string constants) Enclosed in double quotes, for example: "I like C++" –Array of characters, ends with null character '\0' –String is constant pointer Pointer 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' )

Fundamentals of Characters and Strings  cin.getline –Read line of text – cin.getline( array, size, delimiter ); –Copies input into specified array until either One less than size is reached delimiter character is input –Example char sentence[ 80 ]; cin.getline( sentence, 80, '\n' );

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)

String Manipulation Functions of the String-handling Library 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.

String Manipulation Functions of the String-handling Library  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

1 2 // Using strcpy and strncpy. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include // prototypes for strcpy and strncpy 9 10 int main() 11 { 12 char x[] = "Happy Birthday to You"; 13 char y[ 25 ]; 14 char z[ 15 ]; strcpy( y, x ); // copy contents of x into y cout << "The string in array x is: " << x 19 << "\nThe string in array y is: " << y << '\n'; // copy first 14 characters of x into z 22 strncpy( z, x, 14 ); // does not copy null character 23 z[ 14 ] = '\0'; // append '\0' to z's contents cout << "The string in array z is: " << z << endl; contains prototypes for strcpy and strncpy. Copy entire string in array x into array y. Copy first 14 characters of array x into array y. Note that this does not write terminating null character. Append terminating null character.

26 27 return 0; // indicates successful termination } // end main The string in array x is: Happy Birthday to You The string in array y is: Happy Birthday to You The string in array z is: Happy Birthday String to copy. Copied string using strcpy. Copied first 14 characters using strncpy.