Title: Arrays
1Arrays
- Character Arrays and Strings
Alina Solovyova-Vincent Department of Computer
Science Engineering University of Nevada,
Reno Spring 2006
2Worksheet
- char my_word5 c, l, a, s, s
- Is it a character array or a string? Why?
- How can you display it to the screen?
for(i0 ilt5 i) coutltlt my_wordiltlt
3Worksheet
- char my_word6 c, l, a, s, s, \0
- Is it a character array or a string? Why?
- How can you display it to the screen?
coutltlt my_word
4Extraction Operator gtgt (char array)
- char word3
- coutltlt Please enter a 3-letter word // cat
- for ( int i0 i lt 3 i)
- cin gtgt wordi
- coutltltYou have entered
- for ( i0 i lt 3 i)
- cout ltlt wordi // cat
5Extraction Operator gtgt (string)
- char word4
- coutltlt Please enter a 3-letter word // cat
- cin gtgt word
- coutltltYou have entered
- cout ltlt word // cat
6Extraction Operator gtgt (string)
- char word4
- coutltlt Please enter a 3-letter word // cat
- for ( int i0 i lt 3 i)
- cin gtgt wordi
- word3\0 //convert character array into
string - coutltltYou have entered
- cout ltlt word // cat
7Extraction Operator gtgt (char array)
- char phrase10
- coutltlt Please enter a sentence// This is
hard - for ( int i0 i lt 10 i)
- cin gtgt phrasei
- coutltltYou have entered
- for ( i0 i lt 10 i)
- cout ltlt phrasei
// Thisishard
8Extraction Operator gtgt (string)
- char phrase10
- coutltlt Please enter a sentence// This is
hard - cin gtgt phrase
- coutltltYou have entered
- cout ltlt phrase
// This
9getline( )
- char my_string10
- cin.getline(my_string, 10) //user enters hi
- my_string0h
- my_string1i
- my_string2\0
- my_string3 garbage
-
- my_string9 garbage
coutltltmy_string Displayed hi
What is the length of this string???
10getline( )
- char my_string10
- cin.getline(my_string, 10) //user enters I am
happy - my_string0I
- my_string1
- my_string2a
- my_string3m
-
- my_string8p
- my_string9\0
coutltltmy_string Displayed I am happ
11Function get( ) - page 104
- cin.get( )
- reads in a single character at a time. Will read
any character. - if you need to read the ENTER key, a space, or
the tab key, you cannot use cin. You must use
cin.get because cin.get will read any character. - Example
- char c1
- cin.get(c1)
12Function get( )
- char phrase12
- coutltlt Please enter a sentence// This is
hard - for ( int i0 i lt 12 i)
- cin.get(phrasei)
- coutltltYou have entered
- for ( i0 i lt 12 i)
- cout ltlt phrasei
// This is hard
13Initialization of Strings
- char my_word6 c, l, a, s, s, \0
- char my_word6class
- char my_word very interesting class
14Worksheet
- Write a code segment that will prompt the
user for his first name and his last name and
display to the screen - Hello, first_name last_name.
- I.e. If user enters John Smith, the program
should display - Hello, John Smith
15- char first_name20, last_name20
- coutltlt\nEnter your first and last name
- cingtgtfirst_name gtgt last_name
- coutltlt\nHello, ltlt first_name ltlt ltltlast_name
- OR
- char name50
- coutltlt\nEnter your first and last name
- cin.getline(name, 50)
- coutltlt\nHello, ltlt name
16Worksheet
- Write a code segment that will prompt the
user for his first name and his last name and
display to the screen - Hello, last_name first_name.
- I.e. If user enters John Smith, the program
should display - Hello, Smith John.
17Worksheet
- Write a function that will accept two
strings and return true if they are equal in
length and false if they are not.
18Worksheet
- Write a function that will accept an integer
array, its size and a code integer. The
function should find the location of that code
integer in the array and return its position
(index) in the array or 999 if the code was not
found in the array.