Chapter 12 Character Arrays - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Chapter 12 Character Arrays

Description:

sizeof (bb[0] [4]); Lesson 12.3. strlen with 2-D Arrays ... Stops input, terminator read, but not put in array! Reading an Input File ... – PowerPoint PPT presentation

Number of Views:187
Avg rating:3.0/5.0
Slides: 26
Provided by: ralphfto
Category:

less

Transcript and Presenter's Notes

Title: Chapter 12 Character Arrays


1
Chapter 12 Character Arrays
2
Character Arrays
  • Fundamental form of storing text
  • Store C strings (initially developed in C)
  • Store a single character in each element
  • Pointer variables important
  • Declaration of character array char
    array_name size
  • Max number of characters is one less than size
  • Last element holds null (\0) character

Lesson 12.1
3
Initializing Strings
  • Can initialize character by character
    char_array_name value 'x'
  • x is any character
  • character enclosed in single quotes
  • value is subscript referencing an element
  • Last element of character array initialize to
    null zero array_name last '\0'

Lesson 12.1
4
strcpy
  • Function copies string into an array
  • Declaration is in header cstring
  • Must be included to use strcpy

char cc 8 strcpy (cc, " Hello!")
Lesson 12.1
5
Array Memory
Declare a character array
char array_name 8
ERROR!
t e c h n i q u e s \0
Now initialize strcpy (array_name,
"techniques")
Note C does not check array boundaries!
6
Memory
  • Arranged during compilation
  • Order of arrays and variables set
  • Size of block of memory set
  • Reserved during execution
  • Reserved when function is called
  • Stays reserved until function completed then
    memory released

Lesson 12.1
7
Copying Character Arrays
  • Can copy all characters in one array to another
    using strcpy strcpy (array_name1,
    array_name2)
  • Declared size of first array must be large enough
    to hold second array

Remember array name without brackets
represents address of beginning of array
Lesson 12.1
8
strlen Function
  • Used to determine number of characters actually
    stored in character array
  • Uses address of first element to begin search for
    null character
  • Counts number of memory cells strlen
    (address)
  • Address is name of character array

Lesson 12.2
9
sizeof Operator
  • Computes number of bytes reserved for storage
  • General form (parenthesis is optional)
    sizeof (name)
  • name can be variable name or type
  • If type, returns number of bytes used by system
    for that type
  • If array variable name then represents array not
    address of array

Lesson 12.2
10
isupper Function
  • Works with single character
  • Determines whether or not character used is
    uppercase
  • If not uppercase then returns 0
  • If uppercase then returns nonzero integer

isupper (text0)
Lesson 12.2
11
tolower Function
  • Works with single character
  • Returns lowercase version of letter argument
  • Does not modify the argument

tolower (text0)
12
isdigit Function
  • Used to analyze whether particular character in
    string is one of 0-9 digits
  • Helpful for checking input data
  • Returns nonzero integer if 0-9 digit, otherwise
    returns 0

isdigit (character)
Lesson 12.2
13
Character and String Functions
  • Many others available
  • Declarations in ctype header
  • Character functions listed in text, Table 12.1
  • String functions listed in text, Table 12.2

14
2-D Character Arrays
char name num_rows num_cols
  • name can be any valid identifier
  • num_rows and num_cols must be positive integers
  • Example char bb 3 15
  • 45 character-size memory cells

Lesson 12.3
15
Initializing 2-D Arrays
  • Can use strcpy
  • Pass to strcpy address of beginning of each row
  • Example char bb 3 15 strcpy
    (bb0, "The answer ") strcpy (bb1,
    "in the array.")

T h e a n s w e r \0 i n
t h e a r r a y . \0
Lesson 12.3
16
Printing 2-D Arrays
  • Can use cout to print strings stored in 2-D
    arrays
  • In essence, print each row

for (I 0 I lt 3 I) cout ltlt bbj ltlt endl
Lesson 12.3
17
Using sizeof with 2-D Arrays
  • Operand determines declared size
  • Entire array sizeof (bb)
  • Single row sizeof (bb0)
  • Single elements sizeof (bb0 4)

Lesson 12.3
18
strlen with 2-D Arrays
  • Need to pass to strlen the address of beginning
    of each string (row)

strlen (bb0)
Passes address of beginning of first row of bb
and returns number of characters in that
row(excluding terminating null character)
Lesson 12.3
19
Reading Single Word
  • Use cin with the setw manipulator cin gtgt
    setw (sizeof (aa) ) gtgt aa
  • Reads characters until whitespace
  • Limited to numerical value of array size
  • setw manipulator value
  • Size of array used with cin should be at minimum,
    two greater than greatest input
  • Hold whitespace and null character

Lesson 12.4
20
Reading Multiple Lines
  • Use the member function getline
  • Whitespace typed by user included in string

cin.getline (name, num_char, 'terminator')
Lesson 12.4
21
Reading an Input File
  • Can use getline to read entire file

for (j 0 j lt num_lines j)
infile.getline (row_address, num_char)

Remember to include space for two extra
charactersper line end of line and null
Lesson 12.4
22
Extraneous Input Characters
  • More words left behind in buffer after cin
  • ignore member function can be called after cin
    statement

cin.ignore (num_char, 'terminator')
Lesson 12.4
23
Types of Declarations for C Strings
  • One-dimensional character arrays
  • Two-dimensional character arrays
  • Pointers to char
  • Arrays of pointers to char

To allow function to work with any of these
above put address in function call.
Lesson 12.5
24
Passing Strings to Functions
char aa "One-dimensional array." char bb
LENGTH "Two-", "dimensional ",
"array." char cc "Pointer to string
literal." char dd "Array ", "of pointers
", "to string ", "literals."
function1(aa, bb, cc, dd, num_rows_bb,
num_elems_dd) void function1 (char ee , char
ff LENGTH, char gg,
char hh , int num_rows_ff, int num_elems_hh)
Transferring four addresses to function1
Lesson 12.5
25
Summary
Learned how to
  • Manipulate text information using C strings
  • Read text information from a file and keyboard
  • Use Text in arrays and functions
Write a Comment
User Comments (0)
About PowerShow.com