String - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

String

Description:

or separate double quotation '' '' can be used to split string literals into multiple lines ... { G','a','i','n','e','s','v',',i','l','l','e',' ,'A','i','r' ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 16
Provided by: hiy
Learn more at: https://www.cise.ufl.edu
Category:
Tags: string

less

Transcript and Presenter's Notes

Title: String


1
String
  • CGS 3460, Lecture 34
  • Apr 5, 2006
  • Hen-I Yang

2
Previously
  • Dynamic Allocated Arrays
  • Dynamic Allocated Strings
  • Strings

3
Agenda
  • Strings

4
Strings
  • String an array of characters
  • String constant (String literals)
  • String variable

5
String Literals
  • A string that has fixed constant value, e.g. Go
    Gators!
  • \ or separate double quotation can be used
    to split string literals into multiple lines
  • Null character \0 at the end of each string
  • Dont get confused between string literal a and
    character constant a.
  • Behind the scene
  • printf(abc) is handled as
  • char literal abc
  • printf(literal)

6
String variables
  • char strSTR_LEN1
  • any char array
  • end with \0 (null character)
  • difficult to know the length before hand, and
    hard to differentiate between char array and a
    real string
  • why STR_LEN 1?

7
String variables (II)
  • Initialization
  • char location20 Gainesville Airport
  • char location20 G,a,i,n,e,s,v,
    ,i,l,l,e, ,A,i,r,p,o,r,t,
    \0
  • Like any other arrays, if length of the variable
    is less than 20, well fill them up with \0
  • Initialization string cannot be longer than the
    size of the string declared
  • if you declare char location Gainesville
    Airport and not specify its size, can you
    change the size later? No
  • we can also declare char location array and
    pointers are closely related, but are not always
    interchangeable (e.g. location cannot point to
    somewhere else in array version)
  • Always allocate memory before you use the dynamic
    pointer version

8
Reading of Strings
  • char str Clock strikes 12
  • printf(The message s\n, str)
  • we can put the length in the conversion
    specifications
  • Another way puts(str)
  • no conversion specifications,
  • always ends with new line

9
Writing of Strings
  • char str20
  • scanf(s, str)
  • Do we need ?
  • Whitespaces
  • Automatically filled with \0 at the end
  • Problem with reading a whole line
  • User input Clock strikes 12
  • scanf will get only the word Clock
  • gets(str) can actually get the whole line
  • Problem of overflown
  • You can write your own input functions to best
    fit your need
  • read_line example on page 249
  • getchar()

10
String Library
  • What is a library?
  • include ltstring.hgt
  • char strcpy (char s1, const char s2)
  • char str1 abcd
  • char str2 (char ) malloc (5)
  • strcpy(str2, abcd)
  • char strcat(char s1, const char s2)
  • strcpy(str1, efg)
  • char strcmp(const char s1, const char s2)
  • How to they compare?
  • size_t strlen(const char s)

11
String idioms
  • Self-Study
  • Counting the length of a string (observe how the
    code can be made compact)
  • Attach a string at the end of another string

12
Array of Strings
  • How to store it?
  • 2-D arrays or array of pointers
  • Command-Line Arguments
  • main(int argc, char argv)
  • char Argv0 HW5
  • char Argvgt0 arguments
  • Argc is the length of char pointer array Argv

13
By Popular Demand
  • Sample Code
  • Sample Code for Strings

14
Summary
  • Strings

15
Before you go
  • Read Chapter 13
  • Exercise 13.1, 13.2, 13.3, 13.9, 13.11
  • Homework 5 submission on April 11, Tuesday at
    1159 pm
Write a Comment
User Comments (0)
About PowerShow.com