An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 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 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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Strings in C++ 04/29/11. Quiz Arrays  Section 7.1 & 7.2  Array basics  Partially filled arrays  Passing individual elements Mon. 05/02/11.
Chapter 10.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
CS Nov 2006 C-strings.
C-strings Array with base type char One character per indexed variable
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.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
ACS 168 Structured Programming Using the Computer Spring 2002 Joaquin Vila Prepared by Shirley White.
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.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
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.
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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:
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
C++ Programming Basics
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. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Strings, Slide Fundamental Programming Strings.
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2002 Pearson Education, Inc. Slide 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
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.
Suyash Bhardwaj Dept. of Computer Science & Engineering Faculty of Engineering & Technology Gurukul Kangri University, Haridwar l1l1 UNIT 1 String Processing.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
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.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

An Array Type For Strings

Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character marks end of string string New way to process string types Discussed next section

A closer look at cstring An array of characters “Hello” stored as six indexed variables, ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, and the null character Null character used as an end marker, distinct from all ‘real’ characters – ‘\0’ – Is a single character, just like tab or newline How many characters is a c-string variable capable of holding if it is declared as: – char s[10];

Partially filled c-string variables Does not use an int variable to keep track of the number of slots filled Uses the null character to mark the last character in the c-string Draw: “Hi Mom!” if in the array char s[10]; char s[10] = “Hi Mom!”; General declaration: char my_c_string[maxsize + 1] why +1?

Initializations Example 1: char myMessage[20] = “Hi there.”; Example 2: char shortString[] = “abc”; Example 3: char shortString[4] = “abc”; Example 4: char shortString[] = {‘a’, ‘b’, ‘c’}; Are examples 2 and 4 equivalent? Why/why not? Initializing like we did in examples 1, 2, and 3 automatically places the null character in the array at the end of the specified characters If the size is omitted (like in example 2), then the c-string variable will be given a size one character longer than the length of the c- string

Consider the declaration: char ourString[5] = “Hi”; Indexed Variables: – ourString[0] – ourString[1] – ourString[2] – ourString[3] – ourString[4] Write the code that will change ourString to a c-string of the same length consisting of all ‘X’ characters – You should be careful not to replace the null character – Many string manipulating functions depend on the presence of the null character

Using = and == with C strings You can’t use the assignment operator with c-strings as you would with normal variables – Assignment is different from initialization (which is legal) – Ex: char aString[10]; aString = “Hello”; //illegal! The easiest way to assign a value to a c-string variable is to use the strcpy function – Ex: strcpy(aString, “Hello”); – Be careful – this function does not check that the value assigned is within the appropriate length A safer version: strncpy – Takes a third argument that gibes the max number of characters to copy – Ex: char anotherString[10]; strncopy(anotherString, aStringVariable, 9); Instead of using == to test equality of c-string variables, use the function strcmp – Ex: if(strcmp(cString1, cString2)) cout << “strings are not the same” << endl; else cout<< “Strings are the same” << endl;

The library Not needed to declare/initialize c-string variables Needed for many predefined functions needed to process c-string variables

FunctionDescriptionCautions strcpy(target, source)Copies c-string value source into the c-string variable target Does not check to make sure target is large enough to hold value SrcString strncpy(target, source, limit)The same as the two-argument strcpy except that at most limit characters are copied If limit is chosen carefully, this is safer than strcpy. Not implemented in all versions of C++ strcat(target, source)Concatenates the c-string value source onto the end of the c-string in target Does not check to see that target is large enough to hold the result of the concatenation strncat(target, source, limit)The same as the two argument strcat except that at most limit characters are appended Not implemented in all versions of C++ strlen(source)Returns an integer equal to the length of source. The null character is not counted strcmp(string1, string2)Returns 0 (false) if the strings are the same, returns a value 0 if string1 is “more than” string2 (lexicographic order) strncmp(string1, string2, limit)The same as the strcmp except at most limit characters are compared

C-String Arguments and Parameters Keep in mind that a c-string is an array, so it behaves as such as a function parameter Safest to include an int parameter giving the declared size of the c-string variable

Input and Output Can use << for output Can use >> for input, but keep in mind how it behaves with white space – Ex 1 in code Can also use the getline() function for input – Accepts two arguments, the first the c-string variable, the second the size of the c-string – Ex 2 in code

C-String-to-Number conversions FunctionReturn TypePronunciation atoiintA – to – I atollongA – to – L atofdoubleA – to - F All functions accept a c-string argument In See ex. 3 in code