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

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

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

Chapter 10 Characters, Strings, and the string class

System Design and Programming II

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

BITG 1113: Array (Part 2) LECTURE 9

Chapter 8 - Characters and Strings

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.

Fundamentals of Programming. Lecture 11: C Characters and Strings

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

Scientific Programming in C V. Strings

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

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

C: How to Program. Week /May/28

LECTURE 15. String I/O and cstring library

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

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

Chapter 8 C Characters and Strings

today cs3157-fall2002-sklar-lect05 1

Copyright 2003 Pearson Education, Inc. Slide 1

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Engineering Problem Solving with C++, Etter

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

Engineering Problem Solving with C++, Etter/Ingber

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

Chapter 2. Outline. Simple C++ Programs

Strings and Library Functions

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

Exercise 1.1 Hello world

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

C mini reference. 5 Binary numbers 12

Operations. Making Things Happen

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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Strings and Stream I/O

The C++ Language. Arizona State University 1

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS107 Spring 2019, Lecture 4 C Strings

CS107, Lecture 4 C Strings

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

Characters and Strings

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

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

LECTURE 02 INTRODUCTION TO C++

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Object Oriented Programming COP3330 / CGS5409

Chapter 9. Strings. 國立雲林科技大學資訊工程研究所張傳育 (Chuan Yu Chang ) 博士

I/O Streams and Standard I/O Devices (cont d.)

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

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

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

Computer Programming

Chapter 2 Basic Elements of C++

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

Objectives. In this chapter, you will:

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Introduction to string

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

CHAPTER 3 BASIC INSTRUCTION OF C++

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

Computer Science XII Important Concepts for CBSE Examination Questions

Software Design & Programming I

13 4 Understanding Character Strings

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

CS2141 Software Development using C/C++ C++ Basics

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

1 Pointer Concepts. 1.1 Pointer Examples

Structured programming

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

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

Module 4 Arrays, Strings, and Pointers

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Fundamentals of Programming CS-110. Lecture 2

Getting started with C++ (Part 2)

CMSC 202 Midterm Exam 1 Fall 2015

CCE1111 Programming for Engineers [C Programming Language]

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Contents. Preface. Introduction. Introduction to C Programming

What we will learn about this week:

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

SWEN-250 Personal SE. Introduction to C

Computer Programming

Transcription:

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

Objectives Perform character checks and conversions Knock down the C-string fundamentals Point at pointers and C-string library functions Discover C-string definitions and pointer arrays Look at more common programming errors

Character Checks Function isalpha() isalnum() isdigit() islower() isprint() ispunct() isupper() isspace() Meaning True if argument is a letter, false otherwise True if argument is a letter or digit, false otherwise True if argument is a digit, false otherwise True if argument is a lowercase letter, false otherwise True if argument is a printable character, false otherwise True if argument is a punctuation character, false otherwise True if argument is an uppercase character, false otherwise True if argument is a whitespace character, false otherwise

#include <iostream> using namespace std; Character Checks Example int main() { char ch; cout << Input any character: << endl; cin >> ch; if (isalpha(ch)) { cout << ch << is an alphabetic character. << endl; } if (isdigit(ch)) { cout << ch << is a digit. << endl; }

Character Checks Example (2) if (islower(ch)) { cout << ch << " is a lowercase character." << endl; } if (isupper(ch)) { cout << ch << " is an uppercase character." << endl; } if (isspace(ch)) { cout << ch << " is a whitespace character." << endl; } } return 0;

Character Conversion: toupper toupper: if char argument is lowercase letter, return uppercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << toupper(ch1); // displays 'H' cout << toupper(ch2); // displays 'E' cout << toupper(ch3); // displays '!'

Character Conversion: tolower tolower: if char argument is uppercase letter, return lowercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << tolower(ch1); // displays 'h' cout << tolower(ch2); // displays 'e' cout << tolower(ch3); // displays '!'

C-String Fundamentals C++ has two different ways of storing and manipulating strings String class Character strings (C-strings): using an array of characters that is terminated by a sentinel value (the escape sequence '\0 ) Character strings can be manipulated using standard element-byelement array-processing techniques cstring class introduced with latest ANSI/ISO standard

C-String Fundamentals (2) String literal (string): a sequence of characters enclosed in double quotes This is a string Strings stored as an array of characters terminated by a special end-of-string marker called the NULL character This character is a sentinel marking the end of the string The NULL character is represented by the escape sequence,\0

C-String Fundamentals (3) Individual characters in a string array can be input, manipulated, or output using standard array-handling techniques Array-handling techniques can use either subscripts or pointers The end-of-string NULL character is useful for detecting the end of the string

C-String Input and Output Inputting and displaying string requires a standard library function or class method: cin and cout (standard input and output streams) String and character I/O functions Requires the iostream header file Character input methods not the same as methods defined for the string class having the same name Character output methods are the same as for string class

C-String Input and Output Functions C++ Routine Description Example cin.getline (str, n, ch) C-string input from the keyboard cin.getline (str, 81, \n ); cin.get () Character input from the keyboard nextchar = cin.get (); cin.peek () cout.put (charexp) cin.putback (charexp) cin.ignore (n, char) Return the next character of the input stream without extracting it from the stream Place the character on the output stream Push a character back onto the input stream Ignore a maximum of the next n input characters, up to and including the detection of char; if no arguments are specified, ignore the next character on the input stream nextpeek = cin.peek (); cout.put ( A ); cin.putback (ckey); cin.ignore (80, \n ); cin.ignore ();

#include <iostream> using namespace std; C-String Input and Output Example int main() { const int MAXCHARS = 81; char message[maxchars]; // An array large enough to // store a complete line cout << "Enter a string : " << endl; cin.getline(message, MAXCHARS, '\n'); cout << "The message entered is " << message << endl; cin.ignore(); } return 0;

C-String Input and Output Example Sample Run Enter a string: This is a test input of a string of characters. The string just entered is: This is a test input of a string of characters.

Notes About the C-String Input and Output Example The cin.getline() method continuously accepts and stores characters into character array named message Input continues until: Either 80 characters are entered The ENTER key is detected

Notes About the C-String Input and Output Example (2) All characters encountered by cin.getline(), except newline character, are stored in message array Before returning, cin.getline() function appends a NULL character, '\0', to the stored set of characters cout object is used to display the C-string

Reasons for Using a string Class Object Automatic bounds checking on every index used to access string elements The string class automatically expands and contracts storage as needed The string class provides a rich set of methods for operating on a string Easy to convert to a C-string using c_str()

Reasons for Using C- Strings Programmer has ultimate control over how string is stored and manipulated Large number of extremely useful functions exist to input, examine, and process C-strings C-strings are an excellent way to explore advanced programming techniques using pointers (Chapter 14) You will encounter them throughout your programming career, as they are embedded in almost all existing C++ code They are fun to program

C-String Processing C-strings can be manipulated by using either standard library functions or standard array-processing techniques Library functions presented in the next section First look at processing a string in a character-by-character fashion Will allow us to understand how standard library functions are constructed and to create our own library functions Example: strcopy() copies contents of string2 to string1

strcopy () // copy string2 to string1 void strcopy(char string1[], char string2[]) { int i = 0; while ( string2[i]!= '\0') { string1[i] = string2[i]; i++; } string1[i] = '\0'; return; }

Main Features of strcopy () The two strings are passed to strcopy as arrays Each element of string2 is assigned to the equivalent element of string1 until end-of-string marker is encountered Detection of NULL character forces termination of the while loop that controls the copying of elements Because NULL character is not copied from string2 to string1, the last statement in strcopy() appends an end-of-string character to string1

Character-by- Character Input C-strings can be entered and displayed using character-bycharacter techniques We can use cin.get() to accept a string one character at a time Replace cin.getline() function Characters will be read and stored in message array, provided: Number of characters is less than 81 Newline character is not encountered

Pointers and C- String Library Functions Pointers are very useful in constructing functions that manipulate C-strings When pointers are used in place of subscripts to access individual C-string characters, resulting statements are more compact and efficient Consider strcopy() function from a few slides back Two modifications necessary before converting to a pointer version

Possible Modifications of strcopy () Modification 1: eliminate (string2[i]!= \0 ) test from while statement This statement only false when end-of-string character is encountered Test can be replaced by (string2[i]) Modification 2: include assignment inside test portion of while statement Eliminates need to terminate copied string with NULL character

Pointer and C- String Library Function Version of strcopy () void strcopy(char *string1, char *string2) { while (*string1 = *string2) { string1++; string2++; } return; }

Library Functions C++ does not provide built-in operations for complete arrays (such as array assignments) Assignment and relational operations are not provided for C- strings Extensive collections of C-string handling functions and routines included with all C++ compilers These functions and routines provide for C-string assignment, comparison and other operations

Commonly Used Library Functions: strcpy () strcpy(): copies a source C-string expression into a destination C- string variable Example: strcpy(string1, "Hello World!") copies source string literal "Hello World!" into destination C-string variable string1

Commonly Used Library Functions: strcat () strcat(): appends a string expression onto the end of a C-string variable Example: strcat(dest_string, " there World!")

Commonly Used Library Functions: strlen () strlen(): returns the number of characters in its C-string parameter (not including NULL character) Example: value returned by strlen("hello World!") is 12

Commonly Used Library Functions: strcmp () strcmp(): compares two C-string expressions for equality When two C-strings are compared, individual characters are compared a pair at a time If no differences found, strings are equal If a difference is found, string with the first lower character is considered smaller string Example: "Hello" is greater than "Goodbye" (first 'H' in Hello greater than first 'G' in Goodbye)

Character Routines Character-handling routines: provided by C++ compilers in addition to C-string manipulation functions Prototypes for routines are contained in header file cctype; should be included in any program that uses them

Conversion Routines Used to convert strings to and from integer and double-precision data types Prototypes for routines contained in header file cstdlib; cstdlib should be included in any program that uses these routines

String Conversion Routines Prototype Description Example int atoi (stringexp) double atof (stringexp) char [] itoa (stringexp) Convert an ASCII string to an integer; conversion stops at the first noninteger character Convert an ASCII string to a doubleprecision number; conversion stops at the first character that cannot be interpreted as a double Convert an integer to an ASCII string; the space allocated for the returned string must be large enough for the converted value atoi ( 1234 ); atof ( 12.34 ); itoa (1234)

C-String Definitions and Pointer Arrays The definition of a C-string automatically involves a pointer Example: definition char message1[80]; Reserves storage for 80 characters Automatically creates a pointer constant, message1, that contains the address of message1[0] Address associated with the pointer constant cannot be changed It must always point to the beginning of the created array

C-String Definitions and Pointer Arrays (2) Also possible to create C-string using a pointer Example: definition char *message2; creates a pointer to a character message2 is a true pointer variable Once a pointer to a character is defined, assignment statements, such as message2 = "this is a string";, can be made message2, which is a pointer, receives address of the first character in the string

C-String Definitions and Pointer Arrays (3) Main difference in the definitions of message1 as an array and message2 as a pointer is the way the pointer is created char message1[80]explicitly calls for a fixed amount of storage for the array Compiler creates a pointer constant char *message2 explicitly creates a pointer variable first Pointer holds the address of a C-string when the C-string is actually specified

C-String Definitions and Pointer Arrays (4) Defining message2 as a pointer to a character allows C-string assignments message2 = "this is a string"; is valid Similar assignments not allowed for C-strings defined as arrays message1 = "this is a string"; is not valid Both definitions allow initializations using string literals such as: char message1[80] = "this is a string"; char *message2 = "this is a string";

Allocation of space for message1 different from that for message2 C-String Definitions and Pointer Arrays (5) Both initializations cause computer to store same C-string internally message1 storage: Specific set of 80 storage locations reserved; first 17 locations initialized Different C-strings can be stored, but each string overwrites previously stored characters Same is not true for message2

C-String Definitions and Pointer Arrays (6) Definition of message2 reserves enough storage for one pointer Initialization then causes the string literal to be stored in memory Address of the string s first character ( t ) is loaded into the pointer If a later assignment is made to message2, the initial C-string remains in memory; new storage locations allocated to new C-string

C-String Storage Allocation

C-String Storage Allocation (2)

Pointer Arrays Declaration of an array of character pointers is an extremely useful extension to single string pointer declarations Declaration char *seasons[4]; creates an array of four elements; each element is a pointer to a character. Each pointer can be assigned to point to a string using string assignment statements seasons[0] = "Winter"; seasons[1] = "Spring"; seasons[2] = "Summer"; seasons[3] = "Fall"; // note: string lengths may differ

The seasons array does not contain actual strings assigned to the pointers Strings stored in data area allocated to the program Pointer Arrays (2) Array of pointers contains only the addresses of the starting location for each string Initializations of the seasons array can also be put within array definition: char *seasons[4] = {"Winter", "Spring", "Summer", "Fall"};

Pointer Arrays (3)

Using a pointer to point to a nonexistent data element Common Programming Errors Not providing enough storage for a C-string to be stored Misunderstanding of terminology Example: if text is defined as char *text; Variable text is sometimes called a string text is not a string; it is a pointer that contains the address of the first character in the C-string

Summary A C-string is an array of characters that is terminated by the NULL character C-strings can always be processed using standard arrayprocessing techniques The cin, cin.get(), and cin.getline() routines can be used to input a C-string The cout object can be used to display C-strings Pointer notation and pointer arithmetic are useful for manipulating C-string elements

Summary (2) Many standard library functions exist for processing C-strings as a complete unit C-string storage can be created by declaring an array of characters or by declaring and initializing a pointer to a character Arrays can be initialized using a string literal assignment of the form char *arr_name[ ] = "text"; This initialization is equivalent to char *arr_name[ ] = {'t','e','x','t','\0'};