LECTURE 15. String I/O and cstring library

Similar documents
C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Object Oriented Programming COP3330 / CGS5409

Chapter 8: Character & String. In this chapter, you ll learn about;

Introduction to string

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

String Objects: The string class library

Strings. Steven R. Bagley

Assignment 5: MyString COP3330 Fall 2017

C Characters and Strings

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Chapter 10 Characters, Strings, and the string class

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Engineering Problem Solving with C++, Etter

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Chapter 10: Characters, C- Strings, and More About the string Class

System Design and Programming II

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

BITG 1113: Array (Part 2) LECTURE 9

Topic 3. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Copyright 2003 Pearson Education, Inc. Slide 1

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

Characters and Strings

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter 8 - Characters and Strings

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

ONE DIMENSIONAL ARRAYS

C: How to Program. Week /May/28

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

CS11 Advanced C++ Lecture 2 Fall

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

by Pearson Education, Inc. All Rights Reserved.

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

C mini reference. 5 Binary numbers 12

Chapter 8 C Characters and Strings

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Goals of this Lecture

Create a Program in C (Last Class)

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Fundamentals of Programming & Procedural Programming

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

C-String Library Functions

Scientific Programming in C V. Strings

Computers Programming Course 11. Iulian Năstac

8. Characters, Strings and Files

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

1 Pointer Concepts. 1.1 Pointer Examples

13 4 Understanding Character Strings

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Introduction to C/C++ Lecture 5 - String & its Manipulation

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.


C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Exercise 1.1 Hello world

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Chapter 5 - Pointers and Strings

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 5 - Pointers and Strings

Module C++ I/O System Basics

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Name. CPTR246 Spring '17 (100 total points) Exam 2

Computer Programming: Skills & Concepts (CP) Strings

Built-in Functions for NTCAs.

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

EECS402 Lecture 08. Intro To The Standard string Class. Some string Functionality

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Arrays, Strings, & Pointers

CS107 Spring 2019, Lecture 4 C Strings

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Computer Programming

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

CS107, Lecture 4 C Strings

Applied C and C++ Programming

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Chapter 9 Strings. With this array declaration: char s[10];

Lecture 12. We have already used strings. Strings. Hello Class is a string.

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Structured Types. 8. Arrays. collection of components whose organization is characterized by method used to access individual components.

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

The C++ Language. Arizona State University 1

Transcription:

LECTURE 15 String I/O and cstring library

RECAP Recall that a C-style string is a character array that ends with the null character. Character literals in single quotes: 'a', '\n', '$ String literals in double quotes: "Hello World\n Remember that the null-character is implicitly a part of any string literal. The name of an array acts as a pointer to the first element of an array (i.e. it stores the address of where the array starts). Recall that this means when an array is passed into a function, the function has access to the original array contents.

RECAP: THE CCTYPE LIBRARY Recall that this C library contains useful character testing functions, as well as the two conversion functions toupper and tolower. Conversion functions: These return the ascii value of a character. int toupper(int c) - returns the uppercase version of c if it's a lowercase letter, otherwise returns c as is. int tolower(int c) - returns the lowercase version of c if it's an uppercase letter, otherwise returns c as is.

RECAP: THE CCTYPE LIBRARY Query Functions: These all return true (non-zero) or false (0), in answer to the question posed by the function's name. They all take in the ascii value of a character as a parameter. int isdigit(int c) - decides whether the parameter is a digit (0-9). int isalpha(int c) - decides whether the character is a letter (a-z, A-Z). int isalnum(int c) - digit or a letter? int islower(int c) - lowercase digit? (a-z) int isupper(int c) - uppercase digit? (A-Z)

RECAP: THE CCTYPE LIBRARY int isxdigit(int c) - hex digit character? (0-9, a-f). int isspace(int c) - white space character? int iscntrl(int c) - control character? int ispunct(int c) - printing character other than space, letter, digit? int isprint(int c) - printing character (including ' ')? int isgraph(int c) - printing character other than ' ' (space)?

STRING I/O Recall that in the special case of arrays of type char, which are used to implement c- style strings, we can use these special cases with the insertion and extraction operators: char greeting[20] = "Hello, World"; cout << greeting; // prints "Hello, World" char lastname[20]; cin >> lastname; // reads a string into the array 'lastname' // adds the null character automatically

STRING I/O Also remember the following: Using a char array with the insertion operator << will print the contents of the character array, up to the first null character encountered. The extraction operator >> used with a char array will read in a string, and will stop at white space. These examples only apply to the special case of the character array. Clearly, the above cin example is only good for reading one word at a time. What if we want to read in a whole sentence into a string? Well, there are some other library functions worth knowing about.

READING STRINGS: GET AND GETLINE There are two more member functions in class istream (in the iostream library), for reading and storing C-style strings into arrays of type char. Here are the prototypes: char* get(char str[], int length, char delimiter = '\n'); char* getline(char str[], int length, char delimiter = '\n'); Note that this get function is different than the two versions of get we've already seen, which were for reading single characters from an input stream: char ch; ch = cin.get(); cin.get(ch); // extracts one character, returns it // extracts one character, stores in ch

READING STRINGS: GET AND GETLINE The functions get and getline (with the three parameters) will read and store a c-style string. First parameter (str) is the char array where the data will be stored. Note that this is an array passed into a function, so the function has access to modify the original array. Second parameter (length) should always be the size of the array -- i.e. how much storage available. Third parameter (delimiter) is an optional parameter, with the newline as the default. This is the character at which to stop reading. Both of these functions will extract characters from the input stream, but they don't stop at any white space -- they stop at the specified delimiter. They also automatically append the null character, which must (as always) fit into the size of the array.

READING STRINGS: GET AND GETLINE Sample calls: char buffer[80]; cin >> buffer; // reads one word into buffer cin.get(buffer, 80, ',');// reads up to a comma, stores in buffer cin.getline(buffer, 80); // reads an entire line (up to newline) So what is the difference between get and getline? get will leave the delimiter character on the input stream, and it will be seen by the next input statement. getline will extract and discard the delimiter character.

EXAMPLE char greeting[15], name[10], other[20]; cin.getline(greeting,15); // gets input into the greeting array cin.get(name,10,'.'); // gets input into the name array cin.getline(other,20); // gets input into the other array Suppose that the data on the input stream (i.e. typed onto the keyboard, for instance) is: Hello, World Joe Smith. He says hello. At this point, the contents of each string are: greeting: "Hello, World" name: "Joe Smith" other: ". He says hello."

EXAMPLE Here s an example illustrating some different calls that read strings.

THE STANDARD C STRING LIBRARY The standard string library in C is called cstring. To use it, we place the appropriate #include statement in a code file: #include <cstring> This string library contains many useful string manipulation functions. These are all for use with C-style strings. A few of the more commonly used ones are mentioned here.

THE STANDARD C-STRING LIBRARY strlen -- takes one string argument, returns its length (not counting the null character). Prototype: int strlen(const char str[]); Sample calls: char phrase[30] = "Hello, World"; cout << strlen("greetings, Earthling!"); // prints 21 int length = strlen(phrase); // stores 12

THE STANDARD C-STRING LIBRARY strcpy -- takes two string arguments, copies the contents of the second string into the first string. The first parameter is non-constant, the second is constant. Prototype: char* strcpy(char str1[], const char str2[]); //copies str2 into str 1 Sample calls: char buffer[80], firstname[30], lastname[30] = "Smith"; strcpy(firstname, "Billy Joe Bob"); // copies name into firstname strcpy(buffer, lastname); // copies "Smith" into buffer array cout << firstname; // prints "Billy Joe Bob cout << buffer; // prints "Smith"

THE STANDARD C-STRING LIBRARY strcat -- takes two string arguments (first non-constant, second is const), and concatenates the second one onto the first. Prototype: char* strcat(char str1[], const char str2[]); Sample calls: char buffer[80] = "Dog"; char word[] = "food"; strcat(buffer, word); strcat(buffer, " breath"); // buffer is "Dogfood // buffer is "Dogfood breath"

THE STANDARD C-STRING LIBRARY strcmp Takes two string arguments (both passed as const arrays), and returns an integer that indicates their lexicographic order Prototype: int strcmp(const char str1[], const char str2[]); // returns: a negative number if str1 comes before str2, a positive number if str2 comes before str1, 0 // if they are equal. Note: Lexicographic order is by ascii codes. It's NOT the same as alphabetic order. Sample calls: char word1[30] = "apple"; char word2[30] = "apply"; if (strcmp(word1, word2)!= 0) cout << "The words are different\n"; strcmp(word1, word2) strcmp(word1, "apple") strcmp("apple", "Zebra") // returns a negative, means word1 comes first // returns a 0. strings are the same // returns a positive. "Zebra" comes first!

THE STANDARD C-STRING LIBRARY Note that the above calls rely on the null character as the terminator of C-style strings. Remember, there is no built-in bounds checking in C++. strncpy, strncat, strncmp - these do the same as the three listed above, but they take one extra argument (an integer N), and they go up to the null character or up to N characters, whichever is first. Examples: char buffer[80]; char word[11] = "applesauce"; char bigword[] = "antidisestablishmentarianism"; strncpy(buffer, word, 5); // buffer now stores "apple strncat(buffer, " piecemeal", 4); // buffer now stores "apple pie strncmp(buffer, "apple", 5); // returns 0 strncpy(word, bigword, 10); // word is now "antidisest, only had 11 slots! These functions can be used to help do safer string operations. The extra parameter can be included to guarantee that array boundaries are not exceeded, as seen in the last strncpy example.