Strings - PowerPoint PPT Presentation

About This Presentation
Title:

Strings

Description:

Input, output, and other functions will treat the array as a single string, e.g. ... Control characters can also be placed within string constants, e.g. ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 14
Provided by: bman
Category:
Tags: string | strings

less

Transcript and Presenter's Notes

Title: Strings


1
Strings
  • A string is an array of characters terminated by
    a NULL
  • char str15 p,e,a,r,\0 or
    equivalently
  • char str15 pear
  • Input, output, and other functions will treat the
    array as a single string, e.g.
  • cout

2
Important Applications of Strings
Databases and configuration files Input and
output functions Communication Symbolic
computation Computer languages and
compilers Artificial intelligence and expert
systems
3
Common Control Characters
NULL \0 New line \n Horizontal
tab \t Backslash \\ Double quote \ Control
characters can also be placed within string
constants, e.g. cout llo goodbye
4
String Initialization
  • char str15 pear , this statement does
  • Defines a character array variable
  • Allocates memory to store the array
  • Optionally assigns an initial string value
  • we can also define a dimensionless array
  • char str1 pear
  • Initialization constants cannot be used in
    assignments
  • such as str1 pear ? invalid

5
Strings and Arrays
The same caution should be used with strings as
with arrays with regard to boundary and size
checking Trying to access characters outside of
the arrays boundaries will cause stability
problems str1100000000 q is a headache
waiting to happen Trying to store a string that
is too big for an array could also cause problems
6
Common String Functions
strcpy(s1,s2) copies s2 into s1 Useful because
s1 s2 is not a valid assignment include
// header file for string
functions char s15, s25 pear strcpy(s1,s2
) cout 7
Common String Functions
strcat(s1,s2) concatenates s2 onto the end of
s1 Useful for making a larger string from two
smaller ones char s19big , s25
pear strcat(s1,s2) cout pear
8
Common String Functions
strlen(s1) returns the length of s1 Useful for
boundary checking Size does not include the NULL
character char s1 pear cout strlen(s1) ? 4
9
Common String Functions
strcmp(s1,s2) returns 0 if s1 is equal to
s2 _stricmp(s1,s2) same as above but ignores
case Useful for database and lookup table
functions There are many other functions listed
in the VC help index under string manipulation
routines
10
String Type Conversions
Sometimes it is necessary to convert from some
data type such as a double to a string or vice
versa A character array can be defined as an
output or input stream using the ostrstream and
istrstream classes These classes can be used for
type conversions, e.g. include //
header file for classes char s18 ostrstream
sout(s1,8) sout s1 ? 3.14159
11
Multidimensional Arrays
A simple extension to 1D arrays, e.g. int
i122 1,2,3,4 or int i12
1,2,3,4 cout ? 1,2 cout i111 3,4 3D arrays and beyond follow the
same pattern, but watch out for the memory
requirements Total bytes sizeof(int) 22 for
the case above More will be discussed later
along with pointer variables
12
Arrays of Strings
Can be created by using 2D character arrays,
e.g. char s127 apple,orange cout
s11 orange The extension to 2D string
arrays follows the same pattern by using 3D
arrays of characters
13
Standard String Classes
Standard string classes exist that combine string
storage and manipulation functions They offer no
real advantages (in my humble opinion)
Write a Comment
User Comments (0)
About PowerShow.com