Basic Expressions and String Manipulation - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Basic Expressions and String Manipulation

Description:

Basic Expressions and String Manipulation. Week 4. Department of ... b = c = 5; ... String objects are a special type of container, specifically ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 28
Provided by: Too94
Category:

less

Transcript and Presenter's Notes

Title: Basic Expressions and String Manipulation


1
Basic Expressions and String Manipulation
  • Week 4
  • Department of Computer Science
  • Faculty of Science and Technology
  • Assumption University

2
Agenda
  • Basic C Operators
  • Assignment Operators
  • Arithmetic Operators
  • Relational and Equality Operators
  • Logical Operators
  • Mixed Type Expressions
  • Order of Precedence
  • Advanced C Operators
  • Strings and String Manipulation

3
Assignment Operator
  • The assignment operator assigns a value to a
    variable.
  • a 5
  • a b
  • a 2 (b 5)
  • a b c 5

4
Arithmetic Operators
  • The five arithmetical operations supported by the
    C language are
  • OPERATOR MEANING
  • Addition
  • - Subtraction
  • Multiplication
  • / Division
  • Modulo

5
Arithmetic Expression
  • int i1,i2,i3
  • float f1,f2,f3
  • double d1,d2,d3
  • i1 1 f1 1.05 d1 1.05
  • i2 2 f2 2.985 d2 2.985
  • i3 i1 i2 f3 f1 f2 d3 d1 d2
  • i3 i1 - i2 f3 f1 - f2 d3 d1 - d2
  • i3 i1 i2 f3 f1 f2 d3 d1 d2
  • i3 i1 / i2 f3 f1 / f2 d3 d1 / d2
  • i3 i1 i2

6
Mixed Type Expression
  • int n
  • double x
  • x n // is of type double
  • n 4 // is of type int
  • n 4.0 // is of type double

7
Precedence of Operators
  • When writing complex expressions with several
    operands, which operand is evaluated first and
    which later.
  • a 5 7 2
  • We may doubt if it really means
  • a 5 (7 2) // with a result of 6, or
  • a (5 7) 2 // with a result of 0
  • Multiplication (), Division (/), and Modulo ()
    have precedence over Addition () and Subtraction
    (-).

8
Relational and Equality Operators
  • The result of a relational operation is a Boolean
    value that can only be true or false, according
    to its Boolean result.
  • OPERATOR DESCRIPTION
  • Equal to
  • ! Not equal to
  • gt Greater than
  • lt Less than
  • gt Greater than or equal to
  • lt Less than or equal to

9
Relational and Equality Operators
10
Logical Operators
  • The operator ! is the C operator to perform the
    Boolean operation NOT.
  • The operator corresponds with Boolean
    operation AND.
  • The operator corresponds with Boolean
    operation OR.
  • OPERATOR DESCRIPTION
  • ! NOT
  • AND
  • OR

11
Logical Operators
  • !(55) // evaluates to false because (5
    5) is true.
  • !(6lt4) // evaluates to true because (6 lt
    4) is false.
  • !true // evaluates to false
  • !false // evaluates to true.
  • ((55) (3gt6)) // evaluates to false (true
    false).
  • ((55) (3gt6)) // evaluates to true ( true
    false ).

12
Agenda
  • Basic C Operators
  • Advanced C Operators
  • Compound Operators
  • Increase and Decrease
  • Conditional Operator
  • Comma Operator
  • Explicit Type Casting Operator
  • Strings and String Manipulation

13
Compound Assignment
  • We can use compound assignment operators to
    modify the value of a variable
  • a b ? a a b
  • a - b ? a a - b
  • a b ? a a b
  • a / b ? a a / b
  • a b ? a a b

14
Compound Assignment
  • include ltiostreamgt
  • using namespace std
  • int main ()
  • int a, b 3
  • a b
  • a 2 // equivalent to a a 2
  • cout ltlt a
  • return 0

15
Increase and Decrease
  • The increase operator () and the decrease
    operator (--) increase or reduce by one the value
    stored in a variable.
  • They are equivalent to 1 and to -1,
    respectively. c
  • c1
  • cc1

16
Conditional Operator
  • The conditional operator evaluates an expression
    returning a value if that expression is true and
    a different one if the expression is evaluated as
    false. Its format is
  • condition ? result1 result2
  • If condition is true the expression will return
    result1, if it is not it will return result2.

17
Conditional Operator
18
Explicit Type Casting Operator
  • Type casting operators allow you to convert a
    datum of a given type to another.
  • There are several ways to do this in C.
  • int i
  • float f 3.14
  • i (int) f
  • i int (f)

19
Agenda
  • Basic C Operators
  • Advanced C Operators
  • Strings and String Manipulation
  • String
  • String Input
  • String Concatenation
  • Built-in String Functions

20
Strings
  • String objects are a special type of container,
    specifically designed to operate with sequences
    of characters.
  • Unlike traditional c-strings, which are mere
    sequences of characters in a memory array, C
    string objects belong to a class with many
    built-in features to operate with strings in a
    more intuitive way and with some additional
    useful features common to C containers.

21
Strings
  • Before you can use this type, you must bring in
    the string definition with the statement
    include ltstringgt
  • After this you can declare strings like any other
    variable. For example string variable
  • A string constant is any text enclosed in double
    quotes. For example my_name "Steve"

22
Strings
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main ()
  • string str1, str2, str3
  • str1 "Test string " // c-string
  • str2 'x' // single character
  • str3 str1 str2 // string
  • cout ltlt str3 ltlt endl
  • return 0

23
String Input
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main ()
  • string line
  • cout ltlt "Please introduce a text.\n"
  • getline(cin,line)
  • cout ltlt "The text you introduced was\n" ltlt
    line
  • return 0

24
String Concatenation
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main( )
  • string first_name // Declare first string
  • string last_name // Declare second string
  • string full_name // Declare third string
  • first_name "Oliver"
  • last_name "John"
  • // concatenate strings using
  • full_name first_name " " last_name
  • cout ltlt "Full name is " ltlt full_name ltlt '\n'
  • system("PAUSE")
  • return 0

25
String Functions
  • string.size() Return length of string
  • string.length() Return length of string
  • string.capacity() Return size of allocated
    storage
  • string.max_size() Return maximum size of string
  • string.at() Get character in string
  • string.substr() Generate substring
  • string.find() Find content in string

26
String Functions
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main ()
  • string str ("Test string")
  • cout ltlt "size " ltlt str.size() ltlt "\n"
  • cout ltlt "length " ltlt str.length() ltlt "\n"
  • cout ltlt "capacity " ltlt str.capacity() ltlt "\n"
  • cout ltlt "max_size " ltlt str.max_size() ltlt "\n"
  • cout ltlt char at 4 " ltlt str.at(4) ltlt "\n"
  • return 0

27
String Extraction
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main ()
  • string str"We think in generalities, but we
    live in details."
  • string str2, str3
  • str2 str.substr(12,12) // "generalities"
  • pos str.find("live") // position of
    "live" in str
  • str3 str.substr(pos) // get from "live"
    to the end
  • cout ltlt str2 ltlt ' ' ltlt str3 ltlt endl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com