Characters, Strings, and the cstring Library

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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
Object Oriented Programming COP3330 / CGS5409.  Dynamic Allocation in Classes  Review of CStrings.
1 Chapter 10 Characters, Strings, and the string class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
Chapter 8: Character and String CMPD144: Programming 1.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter Characters, Strings, and the string class 10.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Strings: C-strings vs. Strings as Objects
Strings CSCI 112: Programming in C.
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Characters, Strings, and the cstring Library
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Strings A string is a sequence of characters treated as a group
Chapter 12: More on C-Strings and the string Class
«آرايه‌ها و رشته ها».
Standard Version of Starting Out with C++, 4th Edition
Object Oriented Programming COP3330 / CGS5409
Remark: Data Type of Array Name
Strings: C-strings vs. Strings as Objects
10.1 Character Testing.
String in C++.
String What it is Why it’s useful
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Lecture 20 Strings
Strings Skill Area 313 Part C
CS31 Discussion 1D Winter19: week 3
C Characters and Strings
CS31 Discussion 1H Fall18: week 3
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Characters, Strings, and the cstring Library Andy Wang Object Oriented Programming in C++ COP 3330

Recap A C-style string is a char array that ends with the null character Character literals are in single quotes ‘a’, ‘\n’, ‘$’ String literals are in double quotes “Hello world\n” The null character is an implicit part of a string literal

Recap The name of an array points to the first element of an array When an array is passed into a function, the function can access the original array

The cctype Library Conversion functions return the ASCII value of a character int touper(int c) returns the uppercase version of c int tolower(int c) returns the lowercase version of c

Query functions Return true (non-zero) or false (0) to question posed by the function’s name int isdigit(int c) int isalpha(int c) int isalnum(int c) // a digit or a letter int islower(int c); int isupper(int c);

Query functions int isxdigit(int c) // hex digit (0-9, a-f)? int isspace(int c) // white space? 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 other other than ‘ ‘ (space)?

String I/O Insertion (<<) and extraction (>>) char greeting[20] = “Hello, world”; cout << greeting; // prints “Hello, world” char lastname[20]; cin >> lastname; // reads a string into lastname // adds ‘\0’ automatically The insertion operator << will print the content of a char array, up to the first null character The extraction operator >> will read in a string, and stop at a white space

String I/O The cin examples reads one word at a time How to read a whole sentence into a string?

Reading strings: get and getline Two member functions in class istream (in the iostream library) char *get(char str[], int length, char delimiter = ‘\n’); char *getline(char str[], int length, char delimiter = ‘\n’); Note that the get function with only one parameter can only read a single character from the input stream char ch; ch = cin.get(); // extracts one char and returns it cin.get(ch); // extracts one char and stores in ch

get and getline Functions (with Three Parameters) Read and store a c-style string str is the char array where the data will be stored Note that it is passed by reference length is the size of the array delimiter (optional, with ‘\n’ as default) specifies when to stop reading Automatically appends ‘\0’

Example Sample calls char buffer[80]; cin >> buffer; // reads one word into buffer cin.get(buffer, 80, ‘,’); // reads up to the first comma cin.getline(buffer, 80); // reads an entire line (up to endl) So, what is the difference between get and getline? get will leave the delimiter character on the input stream getline will extract and discard the delimiter

Another Example Input Hello, world Joe Smith. He says hello. Code char greeting[15], name[10], other[20]; cin.getline(greeting, 15); // “Hello, world” cin.get(name, 10, ‘.’); // “Joe Smith” cin.getline(other, 20); // “. He says hello.”

Example String Calls http://www.cs.fsu.edu/~myers/c++/examples/strin gs/io1.cpp

io1.cpp #include <iostream> using name space std; int main() { char string1[30], string2[30], string3[30], string4[30]; // print some instructions cin >> string1; // get one word, append it with ‘\0’ cin.getline(string2, 30, ‘,’); // get line, drop ‘,’ cin.get(string3, 30, ‘e’); // get up to ‘e’ cin.get(string4, 30); // get 30 chars

io1.cpp cout << “\n\nOutputs:\n”; cout << “string1 = “ << string1 << endl; cout << “string2 = “ << string2 << endl; cout << “string3 = “ << string3 << endl; cout << “string4 = “ << string4 << endl; return 0; }

Program Execution > To be, or not to be, that is the question.

Program Execution > To be, or not to be, that is the question. Outputs: string1 = To string2 = be string3 = or not to b string4 = e, that is the question.

Standard C String Library To use it, #include <cstring> int strlen(const char str[]); Takes a string argument, returns its length (excluding ‘\0’) Sample calls char phrase[30] = “Hello, world”; cout << strlen(“Greetings, Earthling!”); // prints 21 int length = strlen(phrase); // stores 12

strcpy char *strcpy(char str1[], const char str2[]); Takes two string arguments, and copies the contents of the second string into the first string Sample calls char buffer[80], firstname[30], lastname[30] = “Smith”; strcpy(firstname, “Billy Joe Bob”); strcpy(buffer, lastname); cout << firstname; // prints “Billy Joe Bob” cout << buffer; // prints “Smith”

strcat char *strcat(char str1[], const char str2[]); Takes two string arguments, and concatenates the second one onto the first Sample calls char buffer[80] = “Dog”; char word[] = “food”; strcat(buffer, word); // buffer is now Dogfood strcat(buffer, “ breath”); // buffer is now Dogfood breath

strcmp int strcmp(const char str1[], const char str2[]); Takes two string arguments Returns 0 if they are equal Returns a negative number if str1 comes before str2 (based on ASCII code) Returns a positive number if str2 comes before str1

strcmp Sample Calls char word1[30] = “apple”; char word2[30] = “apply”; if (strcmp(word1, word2) != 0) cout << “The words are different\n”; strcmp(word1, word2); // < 0, word1 comes first strcmp(word1, “apple”); // 0, words are the same strcmp(“apple”, “Zebra”); // > 0, uppercase first

strcmp Note strcmp relies on ‘\0’ at the end of each string No built-in bound checking in C++

strncpy, strncat, strncmp Same as strcpy, strcat, strcmp Takes an additional integer N as an argument Processes up to ‘\0’ or N characters

strncpy, strncat, strncmp Examples char buffer[80]; char word[11] = “applesause”; char bigword[] = “supercalifragilisticexpialidocious”; strncpy(buffer, word, 5); // buffer = “apple” strncat(buffer, “ piecemeal”, 4); // buffer = “apple pie” strncmp(buffer, “apple”, 5); // returns 0 strncpy(word, bigword, 10); // word = “supercalif”