Chars and strings Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Strings Testing for equality with strings.
Advertisements

Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Character and String definitions, algorithms, library functions Characters and Strings.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
CPS120: Introduction to Computer Science Lecture 8.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Introduction to Programming (in C++) Data structures Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Matrices Jordi Cortadella Department of Computer Science.
Numeric Types, Expressions, and Output ROBERT REAVES.
Prime numbers Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Sequences Jordi Cortadella Department of Computer Science.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
CPS120: Introduction to Computer Science
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Data structure design Jordi Cortadella Department of Computer Science.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
Vectors Jordi Cortadella Department of Computer Science.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Characters and Strings
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
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
 Type Called bool  Bool has only two possible values: True and False.
Reasoning with invariants
Jordi Cortadella Department of Computer Science
Primitive Types Vs. Reference Types, Strings, Enumerations
String Encodings and Penny Math
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
The Data Element.
Introduction to Computer Science
CS150 Introduction to Computer Science 1
String Encodings and Penny Math
The Data Element.
Presentation transcript:

Chars and strings Jordi Cortadella Department of Computer Science

Representation of characters (char) Character (char). Represent letters, digits, punctuation marks and control characters. Every character is represented by a code (integer number). There are various standard codes: – American Standard Code for Information Interchange (ASCII) – Unicode (wider than ASCII) Some characters are grouped by families (uppercase letters, lowercase letters and digits). Characters in a family have consecutive codes: 'a'…'z', 'A'…'Z', '0'…'9' Operators: given the integer encoding, arithmetic operators can be used, even though only addition and subtraction make sense, e.g. 'C'+1='D', 'm'+4='q', 'G'-1='F'. Introduction to Programming© Dept. CS, UPC2

Representation of characters (char) Introduction to Programming© Dept. CS, UPC3 ASCII code

Strings Represent sequences of characters. Examples – "Hello, world!", "This is a string", ":-)", "3.1416" – "" is the empty string (no characters) – 'A' is a character, "A" is a string Introduction to Programming© Dept. CS, UPC4

Strings Strings can be treated as vectors of characters. Variables can be declared as follows: – string s1; – string s2 = “abc”; – string s3(10,'x'); Note: use #include in the header of a program using strings. Introduction to Programming© Dept. CS, UPC5

Strings Examples of the operations we can do on strings: – Comparisons: ==, !=,, = Order relation assuming lexicographical order. – Access to an element of the string: s3[i] – Length of a string: s.size() Introduction to Programming© Dept. CS, UPC6

String matching String x appears as a substring of string y at position i if y[i…i+x.size()-1] = x Example: “tree” is the substring of “the tree there” at position 4. Problem: given x and y, return the smallest i such that x is the substring of y at position i. Return -1 if x does not appear in y. Introduction to Programming© Dept. CS, UPC7

String matching Solution: search for such i For every i, check whether x = y[i..i+x.size()-1] In turn, this is a search for a possible mismatch between x and y: a position j where x[j]  y[i+j] If there is no mismatch, we have found the desired i. As soon as a mismatch is found, we proceed to the next i. Introduction to Programming© Dept. CS, UPC8

String matching // Returns the smallest i such that // x == y[i..i+x.size()-1]. // Returns -1 if x does not appear in y int substring(const string& x, const string& y); Introduction to Programming© Dept. CS, UPC9

String matching int substring(const string& x, const string& y) { int i = 0; // Inv: x is not a substring of y at positions 0..i-1 while (i + x.size() <= y.size()) { int j = 0; bool matches = true; // Inv: … and x[0..j-1] == y[i..i+j-1] while (matches and j < x.size()) { matches = (x[j] == y[i + j]); ++j; } if (matches) return i; else ++i; } return -1; } Introduction to Programming© Dept. CS, UPC10

String matching When a mismatch is found (x[j]  y[i+j]) we must proceed to i+1, not to i+j. Example: X = “papageno”, Y = “papapageno” – Consider i=0. – X[0..3] matches Y[0..3], mismatch at X[4]  Y[4] – If we continue searching at i=4, we miss the occurrence of X in Y. Introduction to Programming© Dept. CS, UPC11

String matching A more compact solution with only one loop (but less redable). Introduction to Programming© Dept. CS, UPC12 int substring(const string& x, const string& y) { int i, j; i = j = 0; // Inv: x[0..j-1] == y[i..i+j-1] // and x does not appear in y in positions 0..i-1 while (i + x.size() <= y.size() and j < x.size()) { if (x[j] == y[i + j]) ++j; else { j = 0; ++i; } } if (j == x.size()) return i; else return -1; }

Anagrams An anagram is a pair of sentences (or words) that contain exactly the same letters, even though they may appear in a different order. Example: AVE MARIA, GRATIA PLENA, DOMINUS TECUM VIRGO SERENA, PIA, MUNDA ET IMMACULATA Introduction to Programming© Dept. CS, UPC13

Anagrams Design a function that checks that two strings are an anagram. The function has the following specification: // Returns true if s1 and s2 are an anagram, // and false otherwise. bool anagram(const string& s1, const string& s2); Introduction to Programming© Dept. CS, UPC14

Anagrams A possible strategy for solving the problem could be as follows: – First, we read the first sentence and count the number of occurrences of each letter. The occurrences can be stored in a vector. – Next, we read the second sentence and discount the appearance of each letter. – If a counter becomes negative, the sentences are not an anagram. – At the end, all occurrences must be zero. Introduction to Programming© Dept. CS, UPC15

Anagrams bool anagram(const string& s1, const string& s2) { const int N = int('z') - int('a') + 1; vector count(N, 0); // Read the first sentence for (int i = 0; i = 'a' and c = 'A' and c = 'a' and c = 'A' and c <= 'Z') { // Discount if it is a letter int i = int(c) - int(‘A’); --count[i]; if (count[i] < 0) return false; } } // Check that the two sentences are an anagram for (int i = 0; i < N; ++i) { if (count[i] != 0) return false; } return true; } Introduction to Programming© Dept. CS, UPC16

Summary Strings can be accessed as arrays of chars. String matching (or string searching) is a very frequent operation in file editing and web searching. Recommendation: design algorithms that are independent from the specific encoding of chars. Introduction to Programming© Dept. CS, UPC17