15. Strings Operations Subscripting Concatenation Search Numeric-String Conversions Built-Ins: int2str,num2str, str2double.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
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.
Insight Through Computing 15. Strings Operations Subscripting Concatenation Search Numeric-String Conversions Built-Ins: int2str,num2str, str2double.
Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Strings Selim Aksoy Bilkent University Department of Computer Engineering
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Tutorial 14 Working with Forms and Regular Expressions.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Python for Informatics: Exploring Information
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Stack Applications Qamar Rehman.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
Strings Can view as array of chartacters –Implemented as a class with its own methods Simplest operation on strings is catenating two strings String x.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
String Manipulation Chapter 7 Attaway MATLAB 4E.
CSC 221: Introduction to Programming Fall 2018
Wednesday 09/23/13.
Fundamentals of Python: First Programs
Topics Basic String Operations String Slicing
Introduction to Computer Science
Matlab Basics.
Topics Basic String Operations String Slicing
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Topics Basic String Operations String Slicing
Unit-2 Objects and Classes
Presentation transcript:

15. Strings Operations Subscripting Concatenation Search Numeric-String Conversions Built-Ins: int2str,num2str, str2double

Previous Dealings N = input( ‘Enter Degree:’ ) title(‘The Sine Function’) disp( sprintf(‘N = %2d’,N) )

A String is an Array of Characters x!’ A a 7 * x ! This string has length 9.

Why Important 1.Numerical Data often encoded as strings 2. Genomic calculation/search

Numerical Data is Often Encoded in Strings For example, a file containing Ithaca weather data begins with the string W07629N4226 Longitude: 76 o 29’ West Latitude: 42 o 26’ North

What We Would Like to Do W07629N4226 Get hold of the substring ‘07629’ Convert it to floating format so that it can be involved in numerical calculations.

Format Issues 9 as an IEEE floating point number: 9 as a character: blablahblah otherblabla Different Representation

Genomic Computations Looking for patterns in a DNA sequence: ‘ATTCTGACCTCGATC’ ACCT

Genomic Computations Quantifying Differences: ATTCTGACCTCGATC ATTGCTGACCTCGAT Remove?

Working With Strings

Strings Can Be Assigned to Variables S = ‘N = 2’ N = 2; S = sprintf(‘N = %1d’,N) ‘N = 2’ S sprintf produces a formatted string using fprintf rules

Strings Have a Length s = ‘abc’; n = length(s); % n = 3 s = ‘’; % the empty string n = length(s) % n = 0 s = ‘ ‘; % single blank n = length(s) % n = 1

Concatenation This: S = ‘abc’; T = ‘xy’ R = [S T] is the same as this: R = ‘abcxy’

Repeated Concatenation This: s = ‘’; for k=1:5 s = [s ‘z’]; end is the same as this: z = ‘zzzzz’

Replacing and Appending Characters s = ‘abc’; s(2) = ‘x’ % s = ‘axc’ t = ‘abc’ t(4) = ‘d’ % t = ‘abcd’ v = ‘’ v(5) = ‘x’ % v = ‘ x’

Extracting Substrings s = ‘abcdef’; x = s(3) % x = ‘c’ x = s(2:4) % x = ‘bcd’ x = s(length(s)) % x = ‘f’

Colon Notation s( : ) Starting Location Ending Location

Replacing Substrings s = ‘abcde’; s(2:4) = ‘xyz’ % s = ‘axyze’ s = ‘abcde’ s(2:4) = ‘wxyz’ % Error

Question Time s = ‘abcde’; for k=1:3 s = [ s(4:5) s(1:3)]; end What is the final value of s ? A abcde B. bcdea C. eabcd D. deabc

Problem: DNA Strand x is a string made up of the characters ‘A’, ‘C’, ‘T’, and ‘G’. Construct a string Y obtained from x by replacinig each A by T, each T by A, each C by G, and each G by C x: ACGTTGCAGTTCCATATG y: TGCAACGTCAAGGTATAC

function y = Strand(x) % x is a string consisting of % the characters A, C, T, and G. % y is a string obtained by % replacing A by T, T by A, % C by G and G by C.

Comparing Strings Built-in function strcmp strcmp(s1,s2) is true if the strings s1 and s2 are identical.

How y is Built Up x: ACGTTGCAGTTCCATATG y: TGCAACGTCAAGGTATAC Start: y: ‘’ After 1 pass: y: T After 2 passes: y: TG After 3 passes: y: TGC

for k=1:length(x) if strcmp(x(k),'A') y = [y 'T']; elseif strcmp(x(k),'T') y = [y 'A']; elseif strcmp(x(k),'C') y = [y 'G']; else y = [y 'C']; end

A DNA Search Problem Suppose S and T are strings, e.g., S: ‘ACCT’ T: ‘ATGACCTGA’ We’d like to know if S is a substring of T and if so, where is the first occurrance?

function k = FindCopy(S,T) % S and T are strings. % If S is not a substring of T, % then k=0. % Otherwise, k is the smallest % integer so that S is identical % to T(k:k+length(S)-1).

A DNA Search Problem S: ‘ACCT’ T: ‘ATGACCTGA’ strcmp(S,T(1:4)) False

A DNA Search Problem S: ‘ACCT’ T: ‘ATGACCTGA’ strcmp(S,T(2:5)) False

A DNA Search Problem S: ‘ACCT’ T: ‘ATGACCTGA’ strcmp(S,T(3:6)) False

A DNA Search Problem S: ‘ACCT’ T: ‘ATGACCTGA’ strcmp(S,T(4:7))) True

Pseudocode First = 1; Last = length(S) while S is not identical to T(First;Last) First = First + 1; Last = Last + 1 end

Subscript Error S: ‘ACCT’ T: ‘ATGACTGA’ strcmp(S,T(6:9)) There’s a problem if S is not a substring of T.

Pseudocode First = 1; Last = length(s) while Last<=length(T) && … ~strcmp(S,T(First:Last)) First = First + 1; Last = Last + 1 end

Post-Loop Processing Loop ends when this is false: Last<=length(T) && … ~strcmp(S,T(First:Last))

Post-Loop Processing if Last>length(T) % No Match found k=0; else % There was a match k=First; end The loop ends for one of two reasons.

Numeric/String Conversion

String-to-Numeric Conversion An example… Convention: W07629N4226 Longitude: 76 o 29’ West Latitude: 42 o 26’ North

String-to-Numeric Conversion S = ‘W07629N4226’ s1 = s(2:4); x1 = str2double(s1); s2 = s(5:6); x2 = str2double(s2); Longitude = x1 + x2/60 There are 60 minutes in a degree.

Numeric-to-String Conversion x = 1234; s = int2str(x); % s = ‘1234’ x = pi; s = num2str(x,’%5.3f’); %s =‘3.142’

Problem Given a date in the format ‘mm/dd’ specify the next day in the same format

Y = Tomorrow(x) x y 02/28 03/01 07/13 07/14 12/31 01/01

Get the Day and Month month = str2double(x(1:2)); day = str2double(x(4:5)); Thus, if x = ’02/28’ then month is assigned the numerical value of 2 and day is assigned the numerical value of 28.

L = [ ]; if day+1<=L(month) % Tomorrow is in the same month newDay = day+1; newMonth = month;

L = [ ]; else % Tomorrow is in the next month newDay = 1; if month <12 newMonth = month+1; else newMonth = 1; end

The New Day String Compute newDay (numerical) and convert… d = int2str(newDay); if length(d)==1 d = ['0' d]; end

The New Month String Compute newMonth (numerical) and convert… m = int2str(newMonth); if length(m)==1; m = ['0' m]; end

The Final Concatenation y = [m '/' d];