1 String processing Samuel Marteck ©2010. 2 A character in a string can be accessed by indicating its subscript, also called index. For instance, how.

Slides:



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

Strings Testing for equality with strings.
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.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
CSI 101 Spring 2009 Review and Recap of Visual Basic Wednesday, April 29 th.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Computer Science 111 Fundamentals of Programming I Number Systems.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
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;
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
12/9/2010 Course A201: Introduction to Programming.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Lab 6 Problem 1: DNA. DNA Given a string with length N, determine the number of occurrences of some given substrings (with length K) in that string. For.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Recursion Version 1.0.
Topic 6 Recursion.
While loop statement condition list
CSc 120 Introduction to Computer Programing II Adapted from slides by
Sequences and Indexing
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
String Processing Upsorn Praphamontripong CS 1110
CSc 110, Spring 2017 Lecture 14: Boolean Logic and Midterm Review
Strings Part 1 Taken from notes by Dr. Neil Moore
Announcements 2nd homework is due this week Wednesday (October 18)
CSc 110, Autumn 2016 Lecture 14: Boolean Logic
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
Building Java Programs
Chapter 7: Strings and Characters
Summary of what we learned so far
String Methods: length substring
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 8 More on Strings and Special Methods
Announcements 3rd homework is due this week Wednesday (March 15)
Chapter 8 More on Strings and Special Methods
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
CEV208 Computer Programming
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops.
Puzzle A Puzzle B.
Sequences Sequences are collections of data values that are ordered by position A string is a sequence of characters A list is a sequence of any Python.
Announcements HW1 is due TODAY.
What does this code do? def digitsum(): code goes here def diffsum():
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CMPT 120 Topic: Python strings.
Presentation transcript:

1 String processing Samuel Marteck ©2010

2 A character in a string can be accessed by indicating its subscript, also called index. For instance, how do you get the ‘c’ in s =‘ac453’ ?

3 character‘a’‘c’‘4’‘5’‘3’ index01234

4 How do you get the ‘c’ in s =‘ac453’ ? S[1] = ‘c’ What is the index of the ‘3, the last character in the string?

5 character‘a’‘c’‘4’‘5’‘3’ index01234

6 s =‘ac453’ ? What is the index of the ‘3, the last character in the string? It’s 4, one less than the length of the string

7 The following prints the characters in a string:

8 s = 'abcd' length = len(s) for j in range(length): # 0 <= j <= length -1 print(s[j]) #prints each character in s

9 s = 'abcd' length = len(s) for j in range(length): # 0 <= j <= length -1 print(s[j]) #prints each character in s Since the index of the last character in s is 3 and the last value of the loop index is also 3, no error occurs.

10 For s = 'abcd‘, the length is 4. What would s[4] produce?

11 For s = 'abcd‘, the length is 4. What would s[4] produce? It produces an error message: IndexError: string index out of range

12 In s = 'abcd‘, can you replace the ‘c’ with a ‘z’ by writing s[2] = ‘z’ ?

13 In s = 'abcd‘, can you replace the ‘c’ with a ‘z’ by writing s[2] = ‘z’ ? The answer is no. How would you write a function that replaces every occurrence of a ‘c’ in string with a ‘z’?

14 Let’s write a function to determine if every letter in a word is lowercase.

15 def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False return True When does this return True?

16 def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False return True When does this return True? Only after the entire loop is finished and no non- lowercase letters are found.

17 Why is the following wrong? def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False else: return True

18 def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False else: return True The first time a lowercase letter is found, the function returns True, even if a non- lowercase letter follows it.

19 Checking if ‘asdfdsa’ is a palindrome. Is it symmetric about the middle character?

20 Checking if ‘asdfdsa’ is a palindrome. Its length is 7.

21 Checking if s =‘asdfdsa’ is a palindrome. len(s) = 7 jS[j]k =len-1-jS[k] 0‘a’6

22 Checking if s =‘asdfdsa’ is a palindrome. len(s) = 7 jS[j]k =len-1-jS[k] 0‘a’6 1‘s’5

23 The program is: def palin(s): length = len(s) for j in range(length//2): if s[j ] != s[length - j - 1]: return False #quits the function return True #quits when loop is finished def main(): s = input('type your string \n') if palin(s): print(s + ' is a plaindrome') else: print(s + ' is not a plaindrome') main()

24 Write a function that has a 3-digit int parameter, returns the digit that is the middle one according to size.

25 def ismiddle(n):#returns middle digit according to size s = str(n) a = int(s[0]) b = int(s[1]) c = int(s[2]) small = min(a, b, c ) big = max(a, b, c) sum = a + b + c middle = sum - big - small return middle

26 substrings A substring is a part of a string upto and including the string itself.

27 If s = ‘asdfg’, to get the entire string, use s[0:len(s)].

28 Here len(s) is 5, so s[0:5] gives you the entire string. Note that the 2 nd parameter (here 5) is one more than the index of the last character in the desired string character‘a’‘s’‘d’‘f’‘g’ index01234

29 What would s[1:4] give?

30 character‘a’‘s’‘d’‘f’‘g’ index01234

31 S[2:4] → ‘sdf’ What is S[2:2]?

32 What is S[2:2]? It’s the empty string since the string character indices go from 2 to 1.

33 You can number the characters from the back of the string starting with -1 as is shown next

34 character‘a’‘s’‘d’‘f’‘g’ index

35 How would you write a function to print a string in reverse?

36 You want: The initial value of the loop index to be -1, The increment to be -1 The final value to be the negative of one more than the length since the initial value is -1 and not 0.

37 def reverse(s):#prints a string in reverse n = len(s) for j in range(-1, -(n+1), -1): print(s[j])

38 Write a program that produces a triangle.

39 We’ll start with s = ‘xxxxxxxx’. S[0:1] will give the character at the string index = 0. The entire 8-character string is represented by s[0:9] How do we write the range in the for loop?

40 The entire 8-character string is represented by s[0:9] How do we write the range in the for loop?

41 The entire 8-character string is represented by s[0:9] How do we write the range in the for loop? for j in range(1:10) since the maximum value of j is one less than 10; but the length is 9. How do we write the program?

42 def triangle(s):#produces a triangle n = len(s) for j in range(1, n+1): print(s[0:j])

43 In s[ :len(s)] since the first parameter is blank, it’s assumed that it is 0. So it’s equivalent to s[0 :len(s)]. In s[2: ] since the second parameter is blank, it’s assumed it is the length of the string. So it’s equivalent to s[2 :len(s)].

44 If the second parameter is greater than the length of the string, the second parameter is set to the length of the string. So for a string of length 5, s[1: 10] is equivalent to s[1: 5].