More on Arrays C-Style Strings - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

More on Arrays C-Style Strings

Description:

More on Arrays CStyle Strings – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 21
Provided by: myn91
Category:
Tags: arrays | dptr | more | strings | style

less

Transcript and Presenter's Notes

Title: More on Arrays C-Style Strings


1
More on ArraysC-Style Strings
  • Arrays dealt with so far (in significant depth)
  • Arrays of numbers (int, float, etc.)
  • Strings are just arrays of type char
  • String constant or simply string ? series of
    characters enclosed in double quotes (e.g. "A
    string", "CS 2308")
  • A C-style string is stored as an array of
    characters terminated by a special end-of-string
    marker call null character
  • basic concept for string management in C
    (supported in C)
  • null character ASCII value 0, escape sequence
    \0
  • String initialization (recall) char str "A
    String"
  • Array-handling techniques using subscript or
    pointer notations can be used to input,
    manipulate or output strings
  • string library functions available strings as
    complete entities

2
More on Arrays C-Style Strings (contd)
  • // Character-by-character input output
  • include ltiostream.hgt
  • include ltstdio.hgt
  • void main()
  • char msg81 // enough storage for complete
    line
  • char c
  • int i 0
  • cout ltlt "Enter a string\n"
  • while (i lt 80 (c getchar()) ! '\n')
  • msgi c
  • msgi '\0'
  • cout ltlt "The string just entered is\n"
  • i 0
  • while (msgi ! '\0')
  • cout ltlt msgi

3
More on Arrays C-Style Strings (contd)
  • // Input output using cin and cout
  • include ltiostream.hgt
  • void main()
  • char msg81 // enough storage for complete
    line
  • cout ltlt "Enter a string\n"
  • cin gtgt msg // reads until space or newline
  • cout ltlt "The string just entered is\n"
  • cout ltlt msg

4
More on Arrays C-Style Strings (contd)
  • // Input output using gets() and puts()
  • include ltiostream.hgt
  • include ltstdio.hgt
  • void main()
  • char msg81 // enough storage for complete
    line
  • cout ltlt "Enter a string\n"
  • gets(msg) // reads until newline
  • cout ltlt "The string just entered is\n"
  • puts(msg)

gets()
characters \n
characters \0
puts()
characters \0
characters \n
5
More on Arrays C-Style Strings (contd)
  • // Copy string the hard way
  • // Function to copy srcStr to dstStr
  • // Assumes dstStr has enough space
  • void strCopy(char dstStr, char srcStr) //
    receives 2 arrays
  • int i 0 // i used as subscript
  • while (srcStri ! '\0') // check end of
    source string
  • dstStri srcStri // copy to
    destination string
  • i
  • dstStri '\0' // terminate destination
    string

6
More on Arrays C-Style Strings (contd)
Copy String the Hard Way - Variations
  • void strCopy(char dstStr,
  • char srcStr)
  • int i 0
  • while ( srcStri )
  • dstStri srcStri
  • i
  • dstStri '\0'

void strCopy(char dstStr, char
srcStr) int i 0 while ( dstStri
srcStri ) i
7
More on Arrays C-Style Strings (contd)
Copy String the Hard Way - Variations
void strCopy( char dstPtr, char srcPtr )
while ( dstStr srcPtr ) srcPtr
dstPtr
void strCopy(char dstPtr, char srcPtr)
while ( dstStr srcPtr )
8
More on Arrays C-Style Strings (contd)
  • // Copy string the easy way
  • include ltiostream.hgt
  • include ltstdio.hgt
  • include ltstring.hgt
  • void main()
  • char msgOld81 // enough storage for
    complete line
  • char msgNew81 // enough storage for copy
    of message
  • cout ltlt "Enter a sentence\n"
  • gets(msgOld) // reads until newline
  • strcpy( msgNew , msgOld )
  • cout ltlt "The string just entered is\n"
  • puts(msgOld)
  • cout ltlt "The string just copied is\n"
  • puts(msgNew)

9
More on Arrays C-Style Strings (contd)
  • // Using strlen
  • include ltiostream.hgt
  • include ltstring.hgt
  • void main()
  • char string1 "Boston"
  • char string2 "Tea"
  • char string3 "Party"
  • cout ltlt "The length of string1 is "
  • ltlt strlen(string1)
  • ltlt "\nThe length of string2 is "
  • ltlt strlen(string2)
  • ltlt "\nThe length of string3 is "
  • ltlt strlen(string3) ltlt endl

10
More on Arrays C-Style Strings (contd)
  • // Using strcat
  • include ltiostream.hgt
  • include ltstring.hgt
  • void main()
  • char s120 "Happy "
  • char s2 "New Year "
  • char s340 ""
  • cout ltlt "s1 " ltlt s1
  • ltlt "\ns2 " ltlt s2
  • cout ltlt "\nstrcat(s1, s2) "
  • ltlt strcat(s1, s2)
  • cout ltlt "\nstrcat(s3, s1) "
  • ltlt strcat(s3, s1)

11
More on Arrays C-Style Strings (contd)
  • // Using strcmp
  • include ltiostream.hgt
  • include ltstring.hgt
  • void main()
  • char s1 "Happy New Year"
  • char s2 "Happy New Year"
  • char s3 "Happy Holidays"
  • cout ltlt "s1 " ltlt s1 ltlt "\ns2 " ltlt s2
  • ltlt "\ns3 " ltlt s3
  • cout ltlt "\nstrcmp(s1, s2) " ltlt strcmp(s1,
    s2)
  • cout ltlt "\nstrcmp(s1, s3) " ltlt strcmp(s1,
    s3)
  • cout ltlt "\nstrcmp(s3, s1) " ltlt strcmp(s3,
    s1)

12
More on Arrays C-Style Strings (contd)
Summary
  • String ? array of characters terminated by null
    character
  • Standard array-processing techniques are
    applicable
  • But one would mostly rely on standard library for
    input and display
  • gets() getchar() library functions can be used
    to input string
  • cin gtgt tend to be of limited usefulness for
    string input
  • puts() putchar() can be used to display strings
  • Check out get, getline put member functions of
    stream objects
  • Many standard library functions exist for
    processing strings as complete units internally,
    these functions manipulate strings in
    character-by-character manner, usually using
    pointers
  • ANSI C standard includes a standard string
    class
  • Not covered in this course

13
More on ArraysDynamic (Run-Time) Memory
Allocation
  • Fixed-size arrays as defined in statements like
  • double array20
  • have two drawbacks
  • Memory wasted if array size exceeds number of
    values to be stored
  • Must recompile if array size is less than number
    of values to be stored
  • Root of above drawbacks
  • Memory allocated at compile time (when program is
    being compiled)
  • Mechanism to overcome above drawbacks
  • Dynamic (run-time) memory allocation
  • Two basic operations for dynamic memory
    allocation
  • Acquire (request) additional memory as they are
    needed ? new
  • Release (free) memory when they are no longer
    needed ? delete

14
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • To dynamically (i.e., at run-time) request block
    of memory large enough to hold an object of type
    int (for instance)
  • int intPtr
  • ...
  • intPtr new int
  • ...
  • If request can be granted, new returns address
    of memory allocated
  • If request cannot be granted, new returns the
    null address (ANSI C draft standard has
    different idea) ? value returned by new
    should always be tested, such as
  • if (intPtr 0)
  • cerr ltlt "\n No more memory \n"
  • exit(1)

15
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • To free memory blocks previously acquired (but no
    longer needed)
  • int intPtr
  • ...
  • intPtr new int
  • ...
  • delete intPtr
  • ...
  • Note that delete only frees up memory pointed
    to by intPtr ? it does NOT delete intPtr
    itself
  • Can also initialize object while it is
    dynamically created, for e.g.
  • float piPtr new float(3.14159)
  • In practice, new is rarely used to allocate
    space for scalar values like integers and
    floating-point values
  • Mainly used to allocate space for arrays and
    user-defined data types

16
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • To dynamically (i.e., at run-time) allocate
    memory for an integer array that will hold up to
    10 elements (for instance)
  • int arrayPtr new int10
  • To free up (release) memory allocated for above
    array
  • delete arrayPtr
  • Example code segment
  • cout ltlt "How many entries"
  • cin gtgt numEntries
  • double dPtr new doublenumEntries
  • if (dPtr 0)
  • cerr ltlt "\n No more memory \n"
  • exit(1)

17
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • int main()
  • char charArrayPtrHold 0, charArrayPtr 0,
    oneChar
  • int count 0
  • cout ltlt "Enter some text below" ltlt endl
  • cin.get(oneChar)
  • while (oneChar ! '\n')
  • count
  • charArrayPtrHold charArrayPtr
  • charArrayPtr new char count
  • if (charArrayPtr 0)
  • cerr ltlt "Error allocating memory" ltlt
    endl
  • exit(EXIT_FAILURE)

(continued)
18
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • for (int i 0 i lt count - 1 i)
  • (charArrayPtr i) (charArrayPtrHold
    i)
  • (charArrayPtr count - 1) oneChar
  • delete charArrayPtrHold
  • charArrayPtrHold 0
  • cin.get(oneChar)
  • cout ltlt "The text you entered in reverse is"
    ltlt endl
  • for (int j count - 1 j gt 0 j--)
  • cout ltlt (charArrayPtr j)
  • cout ltlt endl
  • delete charArrayPtr
  • charArrayPtr 0
  • return EXIT_SUCCESS

19
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • What's Wrong with the Following Code Segment?
  • int ptr1 new int
  • if (ptr1 0)
  • cerr ltlt "Error allocating memory" ltlt endl
  • exit(EXIT_FAILURE)
  • int ptr2 0
  • ptr1 100
  • ptr2 ptr1
  • cout ltlt "ptr2 " ltlt ptr2 ltlt endl
  • delete ptr2
  • ptr2 0
  • cout ltlt "ptr1 " ltlt ptr1 ltlt endl

20
More on ArraysDynamic (Run-Time) Memory
Allocation (contd)
  • What's Wrong with the Following Code Segment?
  • do
  • intPtr new int 10
  • if (intPtr 0)
  • cerr ltlt "Error allocating memory" ltlt endl
  • exit(EXIT_FAILURE)
  • // ... dynamic array is used here to solve a
    problem
  • cout ltlt "Do another (y or n)? "
  • cin gtgt answer
  • while (answer ! 'n' answer ! 'N')

Assume all variables have been properly declared
Write a Comment
User Comments (0)
About PowerShow.com