1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.

Slides:



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

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
CPS120: Introduction to Computer Science INPUT/OUTPUT.
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.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Chapter 10.
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Debugging: Catching Bugs ( II ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
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.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Introduction to C programming
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
1 C++ Classes (I) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
CPS120: Introduction to Computer Science Formatted I/O.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
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:
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
 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.
Data Types Storage Size Domain of all possible values Operations 1.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Fundamentals of Characters and Strings
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 2 part #3 C++ Input / Output
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Introduction to cout / cin
Let’s all Repeat Together
Chapter 2 part #3 C++ Input / Output
C++ Programming Lecture 20 Strings
Input/Output Streams, Part 2
Presentation transcript:

1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series

2 An Unsolved Problem Swap the value of two variables Swap( ) a b b a void Swap(int *aa, int *bb); void main( ) { int a = 2, b = 3; Swap(&a, &b); cout << a << “ “ << b; } void Swap(int* aa, int* bb) { int *c; c = aa; aa = bb; bb = c; } Question: Will it work as we expect?

3 What shall we learn today? Using string library –strcmp() –strcpy() –strlen() –atof() Stream Manipulator –setw() –setprecision()

4 Library A Library can be viewed as a toolbox –The implementations are hidden to users. –To make use of it, we only need to know the “interfaces”, i.e., the function prototypes –All the function prototypes can be found in the.h files –Once you use a function of a library, you need to include its.h file –Then, the compiler will know it is a function and reserve it for the linker when producing a.obj file. –Then, the linker will know which library to link with your program to produce a.exe file –There are many standard libraries you can use. You do not need to start from scratch. –You certainly can create libraries of your own.

5 strcmp() int strcmp( const char *string1, const char *string2 ); Return Value The return value indicates the lexicographic relation of string1 to string2. ValueRelationship of string1 to string2 < 0string1 less than string2 = 0string1 identical to string2 > 0string1 greater than string2

6 #include #define SIZE_CL_BUFF 500 void main() { char CL_Buffer[SIZE_CL_BUFF]; while(1){ cout >” ; cin.getline(CL_Buffer, SIZE_CL_BUFF); if(strcmp(CL_Buffer, "quit")==0){ cout << "Thank you and Bye!" << endl; break; } else if(!strcmp(CL_Buffer, "who")){ cout << “I am Bill Gates.\n”; } else{ cout << “Hello world.\n”; }

7 strcpy() char *strcpy( char *strDestination, const char *strSource ); Return Value The function returns the destination string. No return value is reserved to indicate an error. Remarks 1.The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. 2.No overflow checking is performed. 3.The behavior of strcpy is undefined if the source and destination strings overlap.

8 #include void main( void ) { char string[80]; strcpy( string, "Hello world!" ); cout << string << endl; char mystr[] = “ECE230”; cout << strcpy(string, mystr) << endl; // dangerous! char temp[5]; strcpy(temp, mystr); } It is not a good idea to use a longer array for the destination Since it may waste memories What would be a good solution?

9 strlen() size_t strlen( const char *string ); Return Value It returns the number of characters in string, excluding the terminal NULL (i.e., the ‘\0’ character, i.e., 0). Remarks It returns the number of characters in string, not including the terminating null character.

10 #include void main( void ) { char string[80]; strcpy( string, "Hello world!" ); cout << string << endl; char mystr[] = “ECE230”; cout << strcpy(string, mystr) << endl; // Let’s see this solution! char *temp; temp = new char [ strlen(mystr) + 1]; strcpy(temp, mystr); // once you are done with temp delete [] temp; }

11 atof() double atof( const char *string ); int atoi( const char *string); Return Value These returns the double, int value produced by interpreting the input characters as a number. The return value is 0 (for atoi ), or 0.0 (for atof ) if the input cannot be converted to a value of that type.

12 void main() { char *s; double x; int y; s = “ E-15”; x = atof(s); cout << x << endl; s = “-985 pigs”; y = atoi(s); cout << y << endl; } Output: e Woops! How can I get the precision for my floating points?

13 Stream Manipulator stream manipulator capabilities: –setting field widths –setting precisions –setting the fill character in fields –flushing streams –etc.

14 setprecision() precision –member function –sets number of digits to the right of decimal point cout.precision(2); –cout.precision() returns current precision setting setprecision –parameterized stream manipulator –Like all parameterized stream manipulators, required –specify precision: cout << setprecision(2) << x; For both methods, changes last until a different value is set

15 void main() { char *s; s = “ ”; for(int k=1; k<=10; k++){ cout << setprecision(k) << atof(s) << endl; }

16 Setting field widths ios width member function –sets field width (number of character positions, a value should be output or number of characters that should be input) –returns previous width –if values processed are smaller than width, fill characters inserted as padding –values are not truncated - full number printed –cin.width(5); setw stream manipulator cin >> setw(5) >> string; Remember to reserve one space for the null character

17 void main() { double x[] = {2.15, , 3.9}; double y[] = {3.1, 4.11, 5.944}; for(int k=0; k<3; k++) cout << setw(10) << x[k]; cout << endl; for(k=0;k<3;k++) cout << setw(10) << y[k]; cout << endl; } ______2.15____3.1415_______3.9 _______3.1______4.11_____5.944 (note, _ means a space)