Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,

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.
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.
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:
Chapter 10.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
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.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
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.
Introduction to C programming
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
 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. 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)
Strings, Slide Fundamental Programming Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
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.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Strings CSCI 112: Programming in C.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Strings (Continued) Chapter 13
Fundamentals of Characters and Strings
Arrays Arrays exist in almost every computer language.
EGR 2261 Unit 4 Control Structures I: Selection
A First Book of ANSI C Fourth Edition
Strings A string is a sequence of characters treated as a group
Arrays in C.
Object Oriented Programming COP3330 / CGS5409
Arrays and Strings Chapter 9.
C Stuff CS 2308.
Strings.
Introduction to C++ Programming
Pointers and Pointer-Based Strings
Week 9 – Lesson 1 Arrays – Character Strings
Strings A collection of characters taken as a set:
String in C++.
Chapter 3: Input/Output
String What it is Why it’s useful
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Strings What is a string? It is an array of characters terminated with
CPS120: Introduction to Computer Science
7 Arrays.
Exercise Arrays.
C++ Programming Lecture 20 Strings
CS-161 Computer Programming Lecture 15 & 16: Arrays II
CS31 Discussion 1H Fall18: week 6
Chapter 1 c++ structure C++ Input / Output
Dr. Khizar Hayat Associate Prof. of Computer Science
4.1 Introduction Arrays A few types Structures of related data items
Introduction to Problem Solving and Programming
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words, lines of text, C++ programs). Arrays provide a way of doing this, and we can arrays of characters just as we can have arrays of ints and so on. Fundamentally character arrays are no different from other types of arrays. When dealing with characters, however, we typically conform to the convention that stored data be followed by a special “terminator”. An array of char ‘J’ ‘. ’ ‘ ’ ‘B‘ ‘l’ ‘o’ ‘w’ ‘\0’ ? stored data = “J. Blow” terminator The char constant ‘\0’ has code 0, and it and the int constant 0 are therefore effectively equivalent. Programmers often write 0 instead of writing ‘\0’. elements beyond the terminator are unused

Strings A character array used in this fashion is called a “string”. Note that the programmer is quite free to use character arrays in other ways - it is quite permissable, and sometimes useful, to have character arrays that don’t contain a terminator (provided, of course, that they aren’t used in situations which require a string). We have in fact been using strings for some time without realizing it. A string of characters enclosed in double quotes is a string constant. A constant array of char “ABCD” gives ‘A’ ‘B’ ‘C’ ‘D’ ‘\0’ String constants may be used in initializing character arrays. char msg1 [6] = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’. ‘\0’}; // Ow!! char msg2 [6] = “hello”; // much better char msg3[] = “hello”; // better yet - array length is 6

String Output Char arrays are an exception to the rule that arrays may not be output. Outputting a char array causes the characters stored in it to be output, one by one, until a terminator is encountered. char text[] = “hello there”; // character array output (it just happens that the array // is constant). “Greetings!” will appear on the screen cout << “Greetings! “; cout << text; // “hello there” will appear on the screen text[5] = ‘\0’; // replace the blank cout << text; // “hello” will appear If the character array being output does not contain a terminator (is not in fact a string), the output process will continue past the end of the array. This is a programming error.

String Input Char arrays are also an exception to the rule that arrays may not be input. Inputting a char array reads the next non-whitespace sequence (e.g. the next “word”) in the input data. The “word” and a terminator are placed in the array. char str1[20]. str2[20], str3[20]; cout << “Please enter three words: “; cin >> str1 >> str2 >> str3; // if the user enters “I hate 91166” … // str1 will be left containing “I”, // str2 will be left containing “hate”, and // str3 will be left containing “91166” // DO NOT confuse the contents of str3 with the int // value 91166. we are dealing with a collection of // characters that just happen to be numeric digits. If the word to be read (plus the terminator) is longer than the array we’re tring to read into, we have a serious problem (characters will get stored beyond the end of the array).

Setw on Input The problem can be avoided by using the “setw” manipulator. This is the same manipulator we’ve used in output statements, but in input statements it has a different meaning. In an input statement, “setw” limits the number of characters to be read. char a [6]; // the “setw” limits the number of characters // (including the terminator) that can be placed // in “a” to 6 (the size of the array). cin >> setw(6) >> a; If the use of “setw” prevents an input “word” from being completely read, the remaining characters are simply left in the input buffer.

String Operations Strings cannot be assigned using the ‘=‘ operators, or compared using “==“, “!=“, and the other realational operators. There are, however, a number of library functions which allow such operations (and others) to be performed. These functions are made available by including “string.h”. The most useful are listed on p595 of the text. To “assign” one string to another, use function “strcpy”. Its prototype (somewhat simplified for our purposes) is as follows: void strcpy (char dest[], const char src[]); The contents of the source string (up to and including the terminator) are copied intom the destination string. The “const” indicates that the function does not modify the source string, and so allows string constants to be used.

A DIY Strcpy If “strcpy” didn’t exist in the standard libraries, we could easily write it ourselves. void strcpy (char dest[], const char src[]) { int i = 0; // copy everthing up to the terminator while (src[i] != ‘\0’) { dest[i] = src[i]; i++; } dest[i] = ‘\0’; // copy the terminator Note the total lack of protection against the destination string overflowing. If the source string contains more characters (including the terminator) than will fit in the destination, bad things will happen.

A Better Strcpy A “safe” equivalent can be created: void safe_strcpy (char dest[], const char src[], int dest_size) { int i = 0; // copy until we see the terminator or we have // only have one spot left in the destination. while ((src[i] != ‘\0’) && ( i < (dest_size - 1))) { dest[i] = src[i]; i++; } dest[i] = 0; // terminate the destination string The standard library function “strncpy” is similar to the above, but if it runs out of destination space it leaves the destination string unterminated (which is likely to cause problems later on).

Comparing Strings (1) Strings can be compared by using the standard library function “strcmp”. int strcmp (const char str1[], const char str2[]); The function compares the strings and returns -1 if str1 is less than str2 0 if the two strings are identical +1 if str1 is greater than str2 Comparison is performed character by character, starting with the first characters of each string and continuing until either a mismatch is found or the end of one (or both) of the strings is reached. If a mismatch is found, the string with the lower character (based on the usual rules for character comparisons) is less than the other. If the end of one string is reached, the strings are equal if they have the same length. Otherwise the shorter of the strings is less than the longer one.

Comparing Strings (2) Provided that one is dealing only with upper (or lower) case characters, “strcpy” produces the results one might intuitively expect. Thus “CAT” is less than “DOG”, and “rat” is greater than “chipmunk” (just as in a telephone book). When dealing with upper and lower case characters, one must keep in mind that ‘a’ is not equal to ‘A’, and that all of the upper case characters are less than the lower case ones. Thus “ABC” and “abc” are not equal, and “Zoro” is less than “aardvark”. The standard library function “stricmp” works just like “strcmp” except in that it treats upper and lower case characters as being equivalent. Thus “ABC” is equal to “abc” (zero is returned). int strcmp (const char str1[], const char str2[]); “Stricmp” is typically used more than “strcmp”.

Strlen The number of character in a string can be obtained by using the standard library function “strlen”. int strlen (const char str[]); The length DOES NOT include the terminator. strlen(“cat”) is 3 “Strlen” could also be easily written if it didn’t exist (as indeed could “strcmp” and “stricmp”). void strlen (const char str[]) { int i = 0; while (src[i] != ‘\0’) { i++; } return i;

More String Input (1) Using the >> operator to read a string (as in “cin >> str;”) gets us the next non-whitespace sequence (e.g. the next “word”). If we want to read an entire line, blanks and all, we must use a different approach. char a[100]; cin.get (a, 100, ‘\n’); character at whch the input operation is to end. this character is NOT read. array to read characters into size of array = max number of characters to be read + 1. Reading stops when either the specified character (usually ‘\n’) is encountered or the capacity of the array is reached (only one spot left) In either case a terminator is placed after the characters read. The character at which input is to end is NOT read but is instead left in the input buffer. This makes it possible (next slide) to determine whether the whole line was in fact successfully read.

More String Input (2) Another form of “get” can be used to read the next input character (whatever it is, including a blank or ‘\n’). After attempting to read a line (previous slide) the next input character should be ‘\n’. If it isn’t, the input line was too long for our array. if (cin.get() == ‘\n’) { // read next input char // the read ended because ‘\n’ was seen - // eveything is fine . . . } else { // the input line was too long } There is also a “getline” function. It works like “get” (see previous slide) but the “end of read” character is always ‘\n’, and the ‘\n’ is discarded instead of being left in the input buffer. cin.getline (a, 100); // read until ‘\n’ is seen // after the read, we’ve no way of telling // whether or not we got the whole line.