Compiler Design First Lecture.

Slides:



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

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
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.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Character and String definitions, algorithms, library functions Characters and Strings.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
C#: Statements and Methods Based on slides by Joe Hummel.
Chars and strings Jordi Cortadella Department of Computer Science.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Introduction to Java Java Translation Program Structure
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
String and Scanner CS 21a: Introduction to Computing I First Semester,
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Sequences and for loops. Simple for loops A for loop is used to do something with every element of a sequence scala> for (i
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.
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.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The Methods and What You Need to Know for the AP Exam
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
C++ Memory Management – Homework Exercises
C# - Strings.
Chapter 4 – C Program Control
Python unit_4 review Tue/Wed, Dec 1-2
While loop statement condition list
CMSC 202 Lesson 2 C++ Primer.
Lecture 19 Strings and Regular Expressions
Jordi Cortadella Department of Computer Science
Computing with C# and the .NET Framework
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
String Objects & its Methods
Advanced Programming in Java
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Perl Variables: Array Web Programming.
ניתוח מערכות מידע תכנות ב C#
Chapter 8 More on Strings and Special Methods
4.1 Strings ASCII & Processing Strings with the Functions
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Representation and Manipulation
16 Strings.
Lecture 10 Strings CSE /26/2018.
PROGRAMMING IN HASKELL
Introduction to Computer Science
String methods 26-Apr-19.
COMS 261 Computer Science I
By Himanshi dixit 11th ’B’
Just Enough Java 17-May-19.
Switch, Strings, and ArrayLists in Java
What is a String? String s = "compsci"; s c o m p s i
Presentation transcript:

Compiler Design First Lecture

String A string is basically a sequence of characters A string in C# is an object of type String The string type represents a string of Unicode Characters. String objects are immutable that is they cannot be changed after they have been created. 

Example Define "Welcome Word“ as string in c# and print it ? string ST = "Welcome Word"; Console.WriteLine( ST); Console.WriteLine("The String is {0}.", ST); Console.ReadKey();

Example Console.WriteLine("Enter your Message :"); Enter your optional string in c# and print it ? Try enter no. string ST; Console.WriteLine("Enter your Message :"); ST = Console.ReadLine(); Console.WriteLine("The String is {0}.", ST); Console.ReadKey();

Example Enter your optional string in c# and print it and its length ? string S; Console.WriteLine("Enter your Message :"); S = Console.ReadLine(); Console.WriteLine("The String is: {0} and its Length is: {1}.", S, S.Length ); Console.ReadKey();

Example Enter your optional string in c# and print each char in it ? string S; Console.WriteLine("Enter your Message :"); S = Console.ReadLine(); Console.WriteLine(S.Length ); for (int i = 0; i < S.Length; i++) Console.WriteLine(S[i]); Console.ReadKey();

Example Enter your optional string in c# and print second and forth char in it ? String S; Console.WriteLine("Enter your Message :"); S = Console.ReadLine(); Console.WriteLine(S.Length ); Console.WriteLine("The second char is:{0} and the forth char is: {1}", S[1], S[3]); Console.ReadKey();

Example Enter your Message in c# using for loop and special string for stop loop? for (int i = 0; i < 5; i++) { Console.WriteLine("Enter your Message and when you need to stop enter exit"); String line = Console.ReadLine(); if (line == "exit") break; else Console.WriteLine(line.Length); } Console.ReadKey();

Example Print the inverse of string? string Message = "Welcome Dear "; for (int i = 0; i < Message.Length; i++) Console.Write(Message[Message.Length - i - 1]); //try WriteLine Console.WriteLine(); Console.WriteLine (Message); Console.ReadKey();

Example Print the number of words in the string? String s = "hello dear how are you what did do yesterday"; int m = 1; for (int i = 1; i < s.Length; i++) if (s[i] == ' ') m = m + 1; Console.WriteLine(m); Console.ReadKey();

Example Print the char after char ‘d’ in the string? String s = "hello dear how are you what did do yesterday"; for (int i = 1; i < s.Length; i++) if (s[i] == 'd') Console.WriteLine(s[i+1]); Console.ReadKey();

Example Using Foreach This is the easiest, loop. Foreach uses no integer index. it returns each element in order. string s = "Computer Science"; foreach (char c in s) Console.WriteLine(c); Console.ReadKey();

String methods - String.Concat With concat two or more strings become one. It is possible to concatenate two or more strings with several syntax forms. We use the plus operator and the string.Concat method. The plus compiles into string.Concat.

String methods - String.Concat string s1 = "Hello "; string s2 = s1; s1 += "World"; System.Console.WriteLine(s2); //Output: Hello Console.ReadKey();

String methods - String.Concat write C# program that concatenates two strings string s1 = "string2"; string s2 = "string1" + s1; Console.WriteLine(s2); Console.ReadKey();

String methods - String.Concat write C# program that concatenates two strings string s1 = "string2"; string s2 = string.Concat("string1", s1); Console.WriteLine(s2); Console.ReadKey();

String methods - String.Concat write C# program that concatenates three strings string s1 = "string2"; string s2 = "string2"; string s3 = s1 + s2 + "string3"; Console.WriteLine(s3); Console.ReadKey();

String methods - String.Concat write C# program that concatenates four strings string s1 = "string1"; string s2 = "string1" + s1; s2 += "string2"; s2 += s1; Console.WriteLine(s2); Console.ReadKey();

String methods - String.Concat C# program that concats string list var list = new List<string>(); list.Add("cat"); list.Add("dog"); list.Add("perls"); string M = string.Concat(list); Console.WriteLine(M); Console.ReadKey();

String methods - String.Replace Replace. A string contains incorrect chars. It has XYZ but we want ABC. Replace helps with this puzzle. It swaps those substrings. every replacement made results in a new string. To begin, we invoke the Replace method. Please note that we must assign Replace's result to a variable. It does not modify the string in-place.

String methods - String.Replace C# program that uses Replace string input = "_::_pagitica"; Console.WriteLine(input); string output = input.Replace("_::_", "Areo"); Console.WriteLine(output); Console.ReadKey();

String methods - String.Replace C# program that causes multiple replacements const string s = "Dot Net Perls is about Dot Net."; Console.WriteLine(s); string v = s.Replace("Net", "Basket"); Console.WriteLine(v); Console.ReadKey();

String methods - String.IndexOf IndexOf. A string contains many characters. These characters may be searched and tested. We simplify these operations with IndexOf. This method, a string method, returns the first index of a letter in a string. It can also find a substring. It is often used in looping constructs. It returns negative one when nothing is found. We must test for -1 if no value may exist.

String methods - String.IndexOf C# program that uses IndexOf const string value = "Your dog is cute."; if (value.IndexOf("dog") != -1) Console.WriteLine("string contains dog!"); Console.ReadKey();

String methods - String.Compare Compare. This method determines the sort order of strings. It checks if one string is ordered before another when in alphabetical order, whether it is ordered after, or is equivalent.

String methods - String.Compare String Compare method results: String A: First alphabetically String B: Second alphabetically Compare(A, B): -1 Compare(B, A): 1 Compare(A, A): 0 Compare(B, B): 0

String methods - String.Compare C# program that uses Compare string a = "a"; string b = "b"; int c = string.Compare(a, b); Console.WriteLine(c); c = string.Compare(b, a); c = string.Compare(a, a); c = string.Compare(b, b); Console.ReadKey();

String methods - String.Contains Contains. This method searches strings. It checks if one substring is contained in another. Contains returns true or false, not an index.

String methods - String.Contains C# program that uses Contains bool y; string x = "Hello dear How are you now?"; y = x.Contains("dear"); Console.WriteLine(y); Console.ReadKey();

String methods - String.Contains C# program that uses Contains string x = "Hello dear How are you now?"; if (x.Contains("dear")) Console.WriteLine("Good news" ); Console.ReadKey();