Insight Through Computing 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

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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.
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.
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.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering
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.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Strings Selim Aksoy Bilkent University Department of Computer Engineering
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.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
Covenant College September 3, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 6: Character Strings.
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.
Tutorial 14 Working with Forms and Regular Expressions.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CSE123 Lecture 6 String Arrays. Character Strings In Matlab, text is referred to as character strings. String Construction Character strings in Matlab.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Operators, Functions and Modules1 Pattern Matching & Recursion.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
Chars and strings Jordi Cortadella Department of Computer Science.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
Python for Informatics: Exploring Information
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Windows Programming, C.-S. Shieh, KUAS EC, Chapter 3 Operators and Expressions.
Examples of comparing strings. “ABC” = “ABC”? yes “ABC” = “ ABC”? No! note the space up front “ABC” = “abc” ? No! Totally different letters “ABC” = “ABCD”?
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
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,
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 STRINGS String data type Basic operations on strings String functions String procedures.
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:
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
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.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
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.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
15. Strings Operations Subscripting Concatenation Search Numeric-String Conversions Built-Ins: int2str,num2str, str2double.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Primitive Types Four integer types: Two floating-point types:
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
INC 161 , CPE 100 Computer Programming
CS Computer Science IA: Procedural Programming
Jordi Cortadella Department of Computer Science
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
String Manipulation Chapter 7 Attaway MATLAB 4E.
CSC 221: Introduction to Programming Fall 2018
Representation and Manipulation
Fundamentals of Python: First Programs
functions: argument, return value
Introduction to Computer Science
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Unit-2 Objects and Classes
Presentation transcript:

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

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

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

Insight Through Computing Why are Stirngs Important? 1.Numerical Data often encoded as strings 2. Genomic calculation/search

Insight Through Computing 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

Insight Through Computing 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.

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

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

Insight Through Computing Genomic Computations Quantifying Differences: ATTCTGACCTCGATC ATTGCTGACCTCGAT Remove?

Insight Through Computing Working With Strings

Insight Through Computing 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

Insight Through Computing 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

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

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

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

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

Insight Through Computing Colon Notation s( : ) Starting Location Ending Location

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

Insight Through Computing 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

Insight Through Computing 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

Insight Through Computing 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.

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

Insight Through Computing 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

Insight Through Computing 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

Insight Through Computing 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?

Insight Through Computing 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).

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

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

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

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

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

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

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

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

Insight Through Computing 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.

Insight Through Computing Numeric/String Conversion

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

Insight Through Computing 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.

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

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

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

Insight Through Computing 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.

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

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

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

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

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

Insight Through Computing Some other useful string functions str= ‘Cs 1112’; length(str) % 7 isletter(str) % [ ] isspace(str) % [ ] lower(str) % ‘cs 1112’ upper(str) % ‘CS 1112’ ischar(str) % Is str a char array? True (1) strcmp(str(1:2),‘cs’) % Compare strings str(1:2) & ‘cs’. False (0) strcmp(str(1:3),‘CS’) % False (0)

Insight Through Computing ASCII characters (American Standard Code for Information Interchange) ascii code Character:: 65‘A’ 66‘B’ 67‘C’: 90‘Z’: ascii code Character:: 48‘0’ 49‘1’ 50‘2’: 57‘9’:

Insight Through Computing Character vs ASCII code str= ‘Age 19’ %a 1-d array of characters code= double(str) %convert chars to ascii values str1= char(code) %convert ascii values to chars

Insight Through Computing Arithmetic and relational ops on characters ‘c’-‘a’ gives 2 ‘6’-‘5’ gives 1 letter1=‘e’; letter2=‘f’; letter1-letter2 gives -1 ‘c’>’a’ gives true letter1==letter2 gives false ‘A’ + 2 gives 67 char(‘A’+2) gives ‘C’

Insight Through Computing Example: toUpper Write a function toUpper(cha) to convert character cha to upper case if cha is a lower case letter. Return the converted letter. If cha is not a lower case letter, simply return the character cha. Hint: Think about the distance between a letter and the base letter ‘a’ (or ‘A’). E.g., a b c d e f g h … A B C D E F G H … Of course, do not use Matlab function upper ! distance = ‘g’-‘a’ = 6 = ‘G’-‘A’

Insight Through Computing function up = toUpper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; cha is lower case if it is between ‘a’ and ‘z’

Insight Through Computing function up = toUpper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; if ( cha >= 'a' && cha <= 'z' ) % Find distance of cha from ‘a’ end

Insight Through Computing function up = toUpper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; if ( cha >= 'a' && cha <= 'z' ) % Find distance of cha from ‘a’ offset= cha - 'a'; % Go same distance from ‘A’ end

Insight Through Computing function up = toUpper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; if ( cha >= 'a' && cha <= 'z' ) % Find distance of cha from ‘a’ offset= cha - 'a'; % Go same distance from ‘A’ up= char('A' + offset); end