CSc 17 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CSc 17

Description:

Before we begin our discussion on strings, one note related to defining constants ... strcat will concatenate srcStr at the end of destStr. char st1[20] = 'cat' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 22
Provided by: cseLe
Category:
Tags: concatenate | csc

less

Transcript and Presenter's Notes

Title: CSc 17


1
CSc 17
  • Chapter 10 - Strings

2
Constants in Classes
  • Before we begin our discussion on strings, one
    note related to defining constants in classes.
  • You cannot define a constant using the style
    we've seen so far
  • const MAX_NUMS 5

3
Constants in Classes
class MyClass public const MAX_NUMS 5
// illegal!!! int numsMAX_NUMS
4
Constants in Classes
  • There are two ways around this

class MyClass public static const MAX_NUMS
5 // ok int numsMAX_NUMS
class MyClass public enum MAX_NUMS 5
// ok int numsMAX_NUMS
5
Strings
  • We will use two types of strings in this course
  • C-style strings
  • C-style strings

6
C-style Strings ("C Strings")
  • just an array of characters
  • char filename128
  • a special character is used to mark the end of
    the string (a "sentinel") this char takes up one
    of the array entries this means you can only
    store n-1 characters in a C string of dimension n
  • This special character is called the null
    character, has an ASCII code of 0 and is written
    as '\0' (two chars to represent one!)

7
C String Example
  • char filename10
  • 'N','U','M','S','.','T','X','T'
  • N U M S . T X T ? ?

0 1 2 3 4 5 6 7 8 9
C does not guarantee that it will "pad" the
remainder of the string with null characters
8
A Better Way to Initialize
  • char filename10 "NUMS.TXT"
  • N U M S . T X T \0 \0

0 1 2 3 4 5 6 7 8 9
C will "pad" the remainder of the string with
null characters
9
C Strings
  • You can leave off the dimension when using an
    initialization
  • char filename "NUMS.TXT"
  • C will allocate one more than the number of
    chars (to accommodate the null character). So the
    above example is equivalent to
  • char filename9 "NUMS.TXT"

10
Copying C Strings
  • C strings aren't simple variables like integers
    and doubles. You can't assign one string to
    another with
  • char st110 "hello"
  • char st210
  • st2 st1 // illegal!

11
Copying C Strings
  • You can write a function to copy one string to
    another
  • void strCopy(char destStr, char srcStr)
  • int i0
  • do
  • destStri srcStri
  • i
  • while (srcStri ! '\0')
  • char st110 "hello"
  • char st210
  • strCopy(st2, st1) // copy st1 into st2

12
Copying C Strings
  • Actually, there's a C string library that
    contains such a function. It's called strcpy
  • To use this function, include the string library
  • include ltstring.hgt
  • // newer compilers can do
  • // include ltcstringgt

13
Testing Equality with C Strings
  • As with copying one string to another, you can't
    use for comparing one string to another. You
    can do it syntactically, but you get unexpected
    results
  • char st110 "hello"
  • char st210 "hello"
  • if (st1 st2)
  • cout ltlt "EQUAL" ltlt endl
  • else
  • cout ltlt "NOT EQUAL" ltlt endl

14
Testing Equality with C Strings
  • To test equality with C strings, use the strcmp
    function from the string library
  • strcmp(str1, str2)
  • Returns -1 if str1 lt str2
  • Returns 0 if str1 str2
  • Returns 1 if str1 gt str2

15
Other String Library Functions
  • strcat(destStr, srcStr)
  • strcat will concatenate srcStr at the end of
    destStr
  • char st120 "cat"
  • char st210 "in the"
  • char st310 "hat"
  • strcat(st1,st2)
  • strcat(st1,st3)
  • cout ltlt st1 ltlt endl

16
Other String Library Functions
  • strlen(str)
  • strlen returns the length of the string str
  • (loops until it finds the null character)

17
Reading an Entire Line
  • input streams have a member function called
    getline that will read in characters until the
    newline is found or a specified number of
    characters is read (whichever comes first)
  • Example cin.getline(str, 10)

18
Converting Numeric Strings
  • int n atoi(str)
  • converts an integer in C string form to numeric
    integer form (include stdlib.h)
  • int n atoi("205") // returns 205
  • int n atoi("20xy5") // returns 20
  • int n atoi("xy205") // returns 0

19
Converting Numeric Strings
  • long n atol(str)
  • converts a long in C string form to numeric long
    form

20
Converting Numeric Strings
  • double x atof(str)
  • converts a double in C string form to numeric
    double form
  • double x atof("25") // returns 25.0
  • double x atof("20.1.3") // 20.1
  • double x atof("20.1eee3") // 20.1
  • double x atof("20.1e3") // 20100.0

21
Programming
  • Write a function palindrome which takes one
    argument (a C string) and returns true if the
    string is a palindrome, false otherwise
  • Write a program to input integers from the
    keyboard stopping when a blank line is entered
    (use getline and atoi). Output the number of
    lines entered as well as the sum of the numbers
    entered.
Write a Comment
User Comments (0)
About PowerShow.com