NULL. // the character array str has 20 characters; the string uses 9 characters char str[20] = "A String";

Similar documents
Basics of I/O Streams and File I/O

Member Functions of the istream Class

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

C++ Input/Output: Streams

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Passing 1D arrays to functions.

C++ Language Tutorial

An Incomplete C++ Primer. University of Wyoming MA 5310

Computer Programming C++ Classes and Objects 15 th Lecture

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C Strings and Pointers

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Illustration 1: Diagram of program function and data flow

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Using C++ File Streams

Comp151. Definitions & Declarations

Stacks. Linear data structures

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

CS106A, Stanford Handout #38. Strings and Chars

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Appendix K Introduction to Microsoft Visual C++ 6.0

Lecture 5: Java Fundamentals III

Answers to Review Questions Chapter 7

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

C++ INTERVIEW QUESTIONS

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

C++FA 5.1 PRACTICE MID-TERM EXAM

Introduction to Java

Variables, Constants, and Data Types

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Chapter One Introduction to Programming

The little endl that couldn t

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Chapter 2: Elements of Java

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

CS 106 Introduction to Computer Science I

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

13 Classes & Objects with Constructors/Destructors

Course notes Standard C++ programming

El Dorado Union High School District Educational Services

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Install Java Development Kit (JDK) 1.8

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

public static void main(string[] args) { System.out.println("hello, world"); } }

Object Oriented Software Design II

J a v a Quiz (Unit 3, Test 0 Practice)

Python Lists and Loops

Some Scanner Class Methods

Ch 7-1. Object-Oriented Programming and Classes

Introduction to Java. CS 3: Computer Programming in Java

Outline Basic concepts of Python language

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

So far we have considered only numeric processing, i.e. processing of numeric data represented

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

The C Programming Language course syllabus associate level

Object-Oriented Programming in Java

Repetition Using the End of File Condition

A brief introduction to C++ and Interfacing with Excel

Formatting Numbers with C++ Output Streams

Accelerated C++ Practical Programming by Example

Informatica e Sistemi in Tempo Reale

5 Arrays and Pointers

Fondamenti di C++ - Cay Horstmann 1

C++ Essentials. Sharam Hekmat PragSoft Corporation

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Java Interview Questions and Answers

7.7 Case Study: Calculating Depreciation

Binary storage of graphs and related data

Sources: On the Web: Slides will be available on:

Compiler Construction

Sequential Program Execution

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Module 816. File Management in C. M. Campbell 1993 Deakin University

Programming Languages CIS 443

Chapter 13 - The Preprocessor

Java Basics: Data Types, Variables, and Loops

Introduction to Programming II Winter, 2014 Assignment 2

Appendix M: Introduction to Microsoft Visual C Express Edition

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

Introduction to Programming

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Transcription:

C-Style Strings Strings are a fundamental data structure in programming. In this book, we use the standard library string class to handle string objects. Historically, the C programming language defined strings as an array of characters and provided a set of utility functions in the system file string.h. We refer to these string as C-style strings. The array definition of strings was inherited by C++ and woven into the declaration of many classes that are provided by C++ programming environments. You have experience with this fact in the stream open function that requires the file be given as a C-style string. ifstream fin; fin.open("inventory.dat"); // input stream // file name is C-style string As a programmer, you should have some background with C-style strings. Defining a C-style string. A C-style string is a NULL-terminated sequence of characters stored in a C++ array. ASCII 0 designates the NULL character. A string literal, which is a sequence of characters enclosed in double quotes, is a C-style string. The following declaration creates a character array and assigns a string literal to the array. char str[] = "A String"; The string is stored in memory as a 9 element character array. A S t r i n g NULL str Since the string is stored in an array, its name identifies the address of the first character and the location of NULL identifies the end of the string. For instance, str[0] is A and str[8] is the NULL character. The length of a C-style string is the number of characters preceding the NULL character. The length of string str is 8 A program has a variety of options for declaring a C-style string. In each case, we must allocate an array of characters with the NULL character terminating the string. The size of the array and the length of the string are often quite different. // the character array str has 20 characters; the string uses 9 characters char str[20] = "A String"; // str is the NULL string with length 0. str[0] is the NULL character char str[20] = ""; // the compiler allocates four characters including NULL character char str[] = "SOS";

2 EXAMPLE 1 A character array is not a C-style string until it contains a NULL character that indicates the end of the string. char arr[10]; By placing a NULL character in the array block, we create a C-style string. arr[0] = 'C'; arr[1] = '+'; arr[2] = '+'; arr[3] = 0; // arr now contains the string "C++" Converting String and C++ objects. The string class has a constructor that takes a C-style string as an argument. The constructor allows a progra m to convert a C-style string to a string object. The programmer can assign a C-style string to a string object. // argument "Burrito" is a C-style string literal string stra("burrito"), strb; // assigns the C-style string "Chow Mein" to the string object strb strb = "Chow Mein"; Conversion from a string object to a C-style string is provided by the member function c_str() that returns the address of the equivalent C-style string. char *s; string state = "Arizona"; s = state.c_str(); // s is the C-style string "Arizona" This has important applications when opening a file stream. The open statement requires that the file name argument must be a C-style string. string filename("input.dat"); fin.open(filename.c_str()); // return filename as a C-style string C-Style String I/O C++ extends the stream operators "<<" and ">>" to C-style strings. With input, the >> operator extracts the next sequence of characters that are separated by whitespace. The output stream displays all of the characters in the string up to the NULL character. For instance, char word1[32], word2[32]; cin >> word1 >> word2; cout << word2; word1[4] = 0; cout << word1; // Input: baseball umpire // Output: umpire // insert NULL char after e in baseball // Output: base

3 The >> operator uses whitespace characters to separate C-style strings. When we want the input to include whitespace, we must use a special function, getline(), which is a member function in the istream class. The function has three arguments that include a character array, a positive integer n, and a character (delimeter) that terminates input. The delimiter is set to '\n' by default. // member function prototype istream& getline(char inputbuffer[], int n, char delimchar = '\n'); The array inputbuffer will contain the input string. The operation extracts characters from the input stream until either the character delimiter is found, n-1 characters are read, or end-of-file occurs. The characters are stored in the array inputbuffer followed by the NULL character. If delimchar is found, the character is extracted from the stream but not stored in the string. Note that you should not confuse this member function with the free function getline() which we use to input string objects. EXAMPLE 2 1. Read an entire line from the keyboard by using the delimiter '\n'. char line[81]; // space for 80 chars and NULL cin.getline(line,81); // delimeter defaults to '\n' Input: Hello World! H e l l o W o r l d! NULL... line 2. The getline() function uses separate count and delimiter conditions to terminate the input of characters. For parts (a) and (b), assume the input is "GETLINE-DEMO IT". char buffer[25]; (a) fin.getline(buffer,6,'-'); // read 5 chars. append NULL G E T L I NULL (b) fin.getline(buffer,20,'-'); // read to - and discard char G E T L I N E NULL 3. Read a social security number and a name from a stream fin. The social security number is in the format xxx-xx-xxxx and is followed by a colon (:). The name occupies the rest of the line. char ssn[12], name[51]; fin.getline(ssn,12,':'); fin.getline(name,51,'\n'); // read the ssn and discard ':' // rest of the line is the name

4 For input: 459-23-4718:Wilson, John ssn is "459-23-4718" name is "Wilson, John" Note The getline() function must be used carefully with other forms of input. Assume input includes an integer followed by a string on the next line. An attempt to read the string with getline() extracts only the newline character ( ). This causes the input string to be NULL and is a frequent problem when mixing numeric and string input. char inputstr[9]; int n; // incorrect approach cin >> n; // read integer 25 cin.getline(inputstr,9,'\n'); // read and discard Input: 25 Sharon n 25 NULL inputstr Avoid the problem by extracting the newline character with a simple get(). The input for getline() is then taken from the next line. // correct approach! char nl; // declare char object for get() cin >> n; // read integer 25 cin.get(nl); // extract the newline from the stream cin.getline(inputstr,9,'\n'); // read string "Sharon" Input: 25 Sharon n nl 25 S h a r o n NULL inputstr C-style string Handling Functions In the book, we introduce a series of operations for string objects. C-style strings have similar operations provided by free functions in the C++ system file string.h. This section introduces selected functions from the file including strlen() that returns the length of a string and strcmp() that uses ASCII ordering to compare two strings. To modify a string, we discuss functions strcpy() that copies one string to another, strcat() that concatenates two strings, and strstr() that identifies the location of a substring in larger string. For each function, we include the prototype, a description of its action and an example.

5 String Length int strlen(char* s); Action Returns the length of the C-style string s. char s[10] = "Compile"; cout << strlen(s); // output is 7 str[strlen(s)-1] = 0; // eliminate 'e' from s \ String Compare int strcmp(char* s, char* t); Action Compares the relative alphabetic ordering of the C-style strings s and t. Returns a negative value if s is less than t. Returns 0 if s is equal to t. Returns a positive value if s is greater than t. char *s = "township"; int cmpvalue; Pattern Matching (Single Character) // cmpvalue is negative (< 0) since "township" < "village" cmpvalue = strcmp(s,"village"); // cmpvalue is positive (> 0) since "township" > "city" cmpvalue = strcmp(s,"city"); // cmpvalue is 0 since &s[4] is starting address for "ship" cmpvalue = strcmp(&s[4],"ship"); char *strchr(char* s, char ch); Action Returns a pointer to the first occurrence of ch in the C-style string s. Returns NULL if ch is not in s. char *s = "mississippi", *p; p = strchr(s,'s'); p = strchr(s,'t'); // p is the address of s[2] // p is NULL Pattern Matching (Substring) // &s[6] is the starting address for string "sippi" p = strchr(&s[6],'p'); // p is the address of s[8] cout << p; // Output: ppi Action char *strstr(char* s, char* t); Returns a pointer to the first occurrence of string t in the C-style string s. Returns NULL if t is not in s.

6 char *s = "division", *t = "vision"; p = strstr(s,t); // p is the address of s[2] p = strstr(s,"visor"); // p is NULL String Copy char *strcpy(char* s, char* t); Action The function copies string t (including the NULL character) into string s and then returns the address of s. char s[255], *t = "Good Morning", *u = "Evening"; // s = t is an invalid statement, since it attempts to // assign pointer t to constant pointer s. C-style strings // use strcpy() to copy one string to another strcpy(s,t); cout << strcpy(s,u); strcpy(s,&t[5]); // assigns s = "Good Morning" // copy u to s; Output "Evening" // copies "Morning" to s String Concatenate char *strcpy(char* s, char* t); Action Copies string t onto the end of string s and returns the address of s. char s[255] = "Good", *t = "Morning"; Note strcat(s," "); cout << strcat(s,t); // add a blank onto the end of s // append t. s is "Good Morning" The copy and concatenate functions assume that the program provides sufficient memory for the target string. Since the C++ compiler does not perform array bounds checking, errors that are hard to detect may occur. For instance, consider this code. char s[5], t[] = "too big!"; strcpy(s,t); The function copies nine characters to s, but only 5 positions are available. The additional four characters stream into memory, possibly overwriting other program data values. When string objects are used, problems like these do not occur, since the class provides sufficient dynamic memory to hold the result. // string class solution string s, t = "Not too big!"; s = t;

7 PROGRAM ILLUSTRATING C-STYLE STRINGS The program inputs a name into a C-style string in the format "First Last" and copies the name to another string in the format "Last, First". The steps are coded in the function reversename() that takes a pointer to the original string as its first argument and pointer to the new string as its second argument. In a loop, the program input three strings and outputs the reversed string in each case. #include <iostream> #include <string.h> using namespace std; // reverse first and last name and separate them // with a comma. copy result to newname. void reversename(char *name, char *newname); int main() { char name[32], newname[32]; int i; } // read and process three names for (i = 0; i < 3; i++) { cin.getline (name,32,'\n'); reversename(name,newname); cout << "Reversed name: " << newname << endl << endl; } return 0; void reversename(char *name, char *newname) { char *p; // search for first blank in name and replace with NULL char p = strchr(name,' '); *p = 0; // copy last name to newname, append ", " and // concatenate first name strcpy(newname,p+1); strcat(newname,", "); strcat(newname,name); } // replace the NULL char with the original blank *p = ' '; Run: Abraham Lincoln Reversed name: Lincoln, Abraham Debbie Rogers Reversed name: Rogers, Debbie Jim Brady Reversed name: Brady, Jim