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.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
Chapter 9: Arrays and Strings
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
C-strings Array with base type char One character per indexed variable
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.
1 Chapter 10 Characters, Strings, and the string class.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter 15 Strings as Character Arrays
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 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. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Today’s Material Strings Definition Representation Initialization
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Fundamentals of Characters and Strings
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Pointers and Pointer-Based Strings
C++ Programming Lecture 20 Strings
Presentation transcript:

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 are accessed by variables of type char* (pointer to char). For example, if s has type char*, then cout << s << endl; will print all the characters stored in memory beginning at the address s and ending with the first occurrence of the NUL character. The C header file provides a wealth of special functions for manipulating C-strings. For example, the call strlen(s) will return the number of characters in the C-string s, not counting its terminating NUL character. These functions all declare their C-string parameters as pointers to char. So before we study these C-string operations, we need to review pointers.

REVIEW OF POINTERS A pointer is a memory address. For example, the following declarations define n to be an int with value 44 and pn to be a pointer containing the address of n: int n = 44; int* pn = &n; If we imagine memory to be a sequence of bytes with hexadecimal addresses, then we can picture n and pn as shown at right. This shows n stored at the address 64fddc and pn stored at the address 64fde0. The variable n contains value 44 and the variable pn contains the address value 64fddc. The value of pn is the address of n. This relationship is usually represented by a simpler diagram like the one shown at right below. This shows two rectangles, one labeled n and one labeled pn. The rectangles represent storage locations in memory. The variable pn points to the variable n. We can access n through the pointer pn by means of the dereference operator *. For example, the statement *pn = 77; would change the value of n to 77. We can have more than one pointer pointing to the same object: float* q = &x; Now *pn, *q, and x are all names for the same object whose address is 64fddc and whose current value is 77. This is shown in the diagram at right. Here, q is stored at the address 64fde4. The value stored in q is the address 64fddc of n.

C-STRINGS In C++, a C-string is an array of characters with the following important features: An extra component is appended to the end of the array, and its value is set to the NUL character '\0'. This means that the total number of characters in the array is always 1 more than the string length. The C-string may be initialized with a string literal, like this: char str[] = "Bjarne"; Note that this array has 7 elements: 'B', 'j', 'a', 'r', 'n', 'e', and '\0'. The entire C-string may be output as a single object, like this: cout << str; The system will copy characters from str to cout until the NUL character '\0' is encountered. The entire C-string may be input as a single object, like this: cin >> buffer; The system will copy characters from cin into buffer until a white space character is encountered. The user must ensure that buffer is defined to be a character string long enough to hold the input. The functions declared in the header file may be used to manipulate C-strings. These include the string length function strlen(), the string copying functions strcpy() and strncpy(), the string concatenating functions strcat() and strncat(), the string comparing functions strcmp() and strncmp(), and the token extracting function strtok().

EX 2 C-Strings Are Terminated with the NUL Character This little demo program shows that the NUL character '\0' is appended to the C-string: int main() { char s[] = "ABCD"; for (int i = 0; i < 5; i++) cout << "s[" << i << "] = '" << s[i] << "'\n"; } When the NUL character is sent to cout, nothing is printed—not even a blank. This is seen by printing one apostrophe immediately before the character and another apostrophe immediately after the character.

EX3 Ordinary Input and Output of C-Strings This program reads words into a 79-character buffer: int main() { char word[80]; do { cin >> word; if (*word) cout << "\t\"" << word << "\"\n"; } while (*word); } In this run, the while loop iterated 10 times: once for each word entered (including the Ctrl+Z that stopped the loop). Each word in the input stream cin is echoed to the output stream cout. Note that the output stream is not “flushed” until the input stream encounters the end of the line. Each C-string is printed with a double quotation mark " on each side. This character must be designated by the character pair \" inside a C-string literal. The expression *word controls the loop. It is the initial character in the C-string. It will be nonzero (i.e., “true”) as long as the C-string word contains a C-string of length greater than 0. The C-string of length 0, called the empty C-string, contains the NUL character '\0' in its first element. Entering Ctrl+Z+Enter+Entersends the end-of-file character in from cin. This loads the empty C-string into word, setting *word (which is the same as word[0]) to '\0' and stopping the loop. The last line of output shows only the Ctrl+Z echo, as ^Z. The Enter key may have to be pressed twice after Ctrl+Z is entered.

SOME cin MEMBER FUNCTIONS The input stream object cin includes the input functions: cin.getline(), cin.get() cin.ignore(), cin.putback(), and cin.peek(). Each of these function names includes the prefix “cin.” because they are “member functions” of the cin object. –The call cin.getline(str,n) reads up to n characters into str and ignores the rest. –The cin.get() function is used for reading input character-by- character. –The cin.putback() function restores the last character read by a cin.get() back to the input stream cin. –The cin.ignore() function reads past one or more characters in the input stream cin without processing them. –The cin.peek() function can be used in place of the combination cin.get() and cin.putback() functions.

EX8 The cin.putback() and cin.ignore() Functions int nextInt(); int main() { int m = nextInt(),n = nextInt(); cin.ignore(80,'\n'); // ignore rest of input line cout << m << " + " << n << " = " << m+n << endl; } int nextInt() { char ch; int n; while (cin.get(ch)) if (ch >= '0' && ch <= '9') // next character is a digit { cin.putback(ch); // put it back so it can be cin >> n; // read as a complete int break; } return n; } The nextInt() function scans past the characters in cin until it encounters the first digit. In this run, that digit is 3. Since this digit will be part of the first integer 305, it is put back into cin so that the complete integer 305 can be read into n and returned.

STANDARD C CHARACTER FUNCTIONS

STANDARD C STRING FUNCTIONS

The C header file, also called the C-String Library, includes a family of functions that are very useful for manipulating C- strings. EX13 The strlen() Function This program is a simple test driver for the strlen() function. The call strlen(s) simply returns the number of characters in s that precede the first occurrence of the NUL character '\0' #include int main() { char s[] = "ABCDEFG"; cout << "strlen(" << s << ") = " << strlen(s) << endl; cout << "strlen(\"\") = " << strlen("") << endl; char buffer[80]; cout > buffer; cout << "strlen(" << buffer << ") = " << strlen(buffer) << endl; }

EX14 The strchr(), strrchr(), and strstr() Functions #include int main() { char s[] = "The Mississippi is a long river."; cout << "s = \"" << s << "\"\n"; char* p = strchr(s,' '); cout << "strchr(s,' ') points to s[" << p - s << "].\n"; p = strchr(s,'s'); cout << "strchr(s,'s') points to s[" << p - s << "].\n"; p = strrchr(s,'s') ; cout << "strrchr(s,'s') points to s[" << p - s << "].\n"; p = strstr(s,"is") ; cout << "strstr(s,\"is\") points to s[" << p - s << "].\n"; p = strstr(s,"isi" ); if (p == NULL) cout << "strstr(s,\"isi\") returns NULL\n"; }

Excise Run all the example command of this lecture notes to check the results. Before you run it in the visual studio, please first debug it and try to find the output manually.