Title: Lecture 5 Strings
1Lecture 5Strings
- ENGR17 Engineering Programming
- Section 1
- Fall 2001
9/20/01
2Outline
- String Data Type
- Declarations
- Individual Characters
- Loading a String Variable
- String Functions
- Reading a String from Keyboard
3Strings in the Book
- Dont pay too much attention to the chapter in
the book on strings
4String Data Type
- Use Character data type to implement a string
- C does not provide a built-in String data type
that can store string literal constants - Syntax for creating and initializing a String
variable - char variablenamex initialvalue
- x tells C how many one-byte memory locations
to save for use by the String variable - String variables are typically initialized to a
zero-length string (double quotes with no space
between ) - Syntax for creating a String named constant
- const char constantnamex expression
- A special character marks the end of a string
- Null Character \0
- This character takes up one space
5Declaring a String
char name3 ""
\0
char name5 "Jon"
\0
J
o
n
const char NAME7 "Jon A"
\0
J
o
n
A
Cant be changed because it is a named constant
6Accessing Individual Characters
Double Quotes
Length
char name5 ""
\0
Index
name0 J name1 o name2
n name3 \0
\0
J
o
n
Single Quotes for single chars
7Loading a String Variable
Need this to use strcpy
include ltstring.hgt void main () char name5
"" name "Jon" strcpy(name,"Jon")
\0
Illegal
\0
J
o
n
8String Functions
All of these functions require include ltstring.hgt
9String Function Examples
char str110 "abc" char str210 "xyz"
10Reading a String from Keyboard
- Use cin streams getline member function
- Reads an entire line of text, including the
spaces - object.getline(variablename, length of string
variable) - The period is the dot member selection operator
- Use the sizeof() operator to get total total
string size - cin.getline(name, sizeof(name))
- Difference between strlen() and sizeof()
- char name100 Jon
- int length
- length strlen(name) // length is 3
- length sizeof(name) // length is 100
11Read a String from Keyboard
- include ltiostream.hgt
- int main ()
- char name100 // string of length 100
- cout ltlt Enter name
- cin.getline(name,sizeof(name))
- cout ltlt Name is ltlt name ltlt endl
This is different than how the book does it. Read
a string this way ? not like the book! Be sure to
use sizeof()
12Summary
- Declare and initialize a string
- Modify individual characters
- Load a string
- String functions
- Read string from keyboard
13Questions
- Declare a string of size 100 and initialize it to
empty. - char name100
- Declare a constant string and initialize it to
test. - const char name test
- Char name5 Jon //change the 2nd letter to
an a. - name1 a
- Char str15 abc Char str25 def
- Result of Strcpy(str2,str1)?
- Str2 abc
- Result of strcat(str2,str1)?
- Str2 defabc
- Result of strlen(str2)?
- 3
14Questions
- What do you need at the beginning of the program
to use the string functions? - include ltstring.hgt
- Whats the line to read a sting in from the
keyboard? - cin.getline(name, sizeof(name))
- Whats the difference between sizeof() and
strlen()? - Sizeof gives you the entire length of the
variable and strlen gives you the length of the
string loaded into the variable - Sizeof is an operator strlen is a built-in
function - Do you need include ltstring.hgt for sizeof()?
- No
15Example
- Write a program to read in a users first and
last name. Build a new string in the format N
ltlastgt, ltfirstgt and print it to the screen.
Print the length of the string to the screen.
Check if the first and last name are the same and
print the result to the screen.