Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering

Slides:



Advertisements
Similar presentations
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Advertisements

User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
DREAM PLAN IDEA IMPLEMENTATION. 2 Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani
Chapter 7. Even in quantitative sciences, we often encounter letters and/or words that must be processed by code You may want to write code that: Reads.
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:
String Functions. Getting Numeric Equivalents of Chars >> double('abc xyz') ans = >> double('ABC XYZ') ans =
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Review Selim Aksoy Bilkent University Department of Computer Engineering
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
Top-down Program Design, Relational and Logical Operators
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Top-down Program Design Selim Aksoy Bilkent University Department of Computer Engineering
Program Design, Relational and Logical Operators Selim Aksoy Bilkent University Department of Computer Engineering
CS Nov 2006 C-strings.
Insight Through Computing 15. Strings Operations Subscripting Concatenation Search Numeric-String Conversions Built-Ins: int2str,num2str, str2double.
Additional Data Types: 2-D Arrays, Logical Arrays Selim Aksoy Bilkent University Department of Computer Engineering
Introduction to MATLAB
MATLAB Strings Selim Aksoy Bilkent University Department of Computer Engineering
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Covenant College September 3, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 6: Character Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Text Processing. Outline Announcements: –Homework I: due Today. by 5, by Discuss on Friday. –Homework II: on web HW I: question 7 Finish functions.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CSE123 Lecture 6 String Arrays. Character Strings In Matlab, text is referred to as character strings. String Construction Character strings in Matlab.
Numeric Types, Expressions, and Output ROBERT REAVES.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
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.
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.
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.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
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.
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.
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.
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.
Input/Output Functions. 2 MATLAB Basics: Data Files save filename var1 var2 … save homework.mat x y  binary save x.dat x – ascii  ascii load filename.
Introduction to programming in java
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Chapter 8 - Characters and Strings
String Manipulation Chapter 7 Attaway MATLAB 4E.
C++ Programming Lecture 20 Strings
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering

Spring 2004CS 1112 Strings A string is an array of characters s = 'abc' is equivalent to s = [ 'a' 'b' 'c' ] All operations that apply to vectors and arrays can be used together with strings as well s(1)  'a' s( [ 1 2 ] ) = 'XX'  s = 'XXc' s(end)  'c'

Spring 2004CS 1113 String Conversion Conversion of strings to numerical arrays double( 'abc xyz' ) ans = double( 'ABC XYZ' ) ans = Conversion of numerical arrays to strings char( [ ] ) ans = Hello!

Spring 2004CS 1114 Character Arrays 2-D character arrays s = [ 'my first string'; 'my second string' ] ??? Error s = char( 'my first string', 'my second string' ) s = my first string my second string size(s)  [ 2 16 ] size( deblank( s(1,:) ) )  [ 1 15 ] char function automatically pads strings

Spring 2004CS 1115 String Tests ischar() : returns 1 for a character array ischar ( 'CS 111' ) ans = 1 isletter() : returns 1 for letters of the alphabet isletter( 'CS 111' ) ans = isspace() : returns 1 for whitespace (blank, tab, new line) isspace( 'CS 111' ) ans =

Spring 2004CS 1116 String Comparison Comparing two characters 'a' < 'e' ans = 1 Comparing two strings character by character 'fate' == 'cake' ans = 'fate' > 'cake' ans =

Spring 2004CS 1117 String Comparison strcmp() : returns 1 if two strings are identical a = 'Bilkent'; strcmp( a, 'Bilkent' ) ans = 1 strcmp( 'Hello', 'hello' ) ans = 0 strcmpi() : returns 1 if two strings are identical ignoring case strcmpi( 'Hello', 'hello' ) ans = 1

Spring 2004CS 1118 String Case Conversion Lowercase-to-uppercase a = upper( 'This is test 1!' ) a = THIS IS TEST 1! Uppercase-to-lowercase a = lower( 'This is test 1!' ) a = this is test 1!

Spring 2004CS 1119 Searching in Strings findstr() : finds one string within another one test = 'This is a test!'; pos = findstr( test, 'is' ) pos = 3 6 pos = findstr( test, ' ' ) pos =

Spring 2004CS Searching in Strings strtok() : finds a token in a string [ token, remainder ] = strtok( 'This is a test!', ' ' ) token = This remainder = is a test! remainder = 'This is a test!'; while ( any( remainder ) ), [ word, remainder ] = strtok( remainder ); disp( word ); end

Spring 2004CS Replacing in Strings strrep() : replaces one string with another s1 = 'This is a good example'; s2 = strrep( s1, 'good', 'great' ) s2 = This is a great example

Spring 2004CS String Conversion Recall num2str() for numeric-to-string conversion str = [ 'Plot for x = ' num2str( 10.3 ) ] str = Plot for x = 10.3 str2num() : converts strings containing numbers to numeric form x = str2num( '3.1415' ) x =

Spring 2004CS String Conversion sprintf() is identical to fprintf() but output is a string str = sprintf( 'Plot for angle = %0.4f', pi ) str = Plot for angle =

Spring 2004CS String Evaluation eval() : evaluates the input string eval( '2 * 11^2 / 5' ) ans = eval( 'sqrt( 9.32 ) + sin( 3 * pi / 4 )' ) ans = x = 12; eval( [ '3 * ' num2str(x) ' + 5' ] ) ans = 41

Spring 2004CS String Comparison Example: Write a function that takes two strings and returns -1 if the first string is lexicographically less than the second 0 if they are equal +1 if the first string is lexicographically greater than the second Pseudocode: Get input strings Pad strings to equal length Compare characters from beginning to end and find the first difference Return a value depending on the first distance

Spring 2004CS String Comparison function result = c_strcmp(str1,str2) %C_STRCMP Compare strings like C function "strcmp" % Function C_STRCMP compares two strings, and returns % a -1 of str1 < str2, a 0 if str1 == str2, and a % +1 if str1 > str2. % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 10/18/98 S. J. Chapman Original code % Check for a legal number of input arguments. msg = nargchk(2,2,nargin); error(msg); % Check to see if the arguments are strings if ~(isstr(str1) & isstr(str2)) error('Both str1 and str2 must both be strings!') else % Pad strings strings = strvcat(str1,str2); % Compare strings diff = strings(1,:) ~= strings(2,:); if sum(diff) == 0 % Strings match, so return a zero! result = 0; else % Find first difference between strings ival = find(diff); if strings(1,ival(1)) > strings(2,ival(1)) result = 1; else result = -1; end