Characters and Strings Outline Declaration: Initialization Special

advertisement
Outline
Characters and Strings
n
Character Arrays and Strings
n
n
n
n
by Ziad Kobti
n
n
n
n
n
Declaration:
n
Declare a character (char) array the same
way you declare arrays.
Initialization
n
Arrays may be initialized at declaration time.
Example:
char myArray[10];
n
n
In C there is no native String data type.
Strings are simply defined as a null
terminated ‘array of characters’.
Strings and string manipulatins are therefore
applications of arrays and of functions that
work on these arrays.
Special Case
n
Although char arrays may be used to store
strings such words and sentences, we need
to explicitely terminate the array with a
special character symbol :
char myArray[ 5 ] = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ };
or
char Word[ ] = { ‘n’, ‘a’, ‘t’, ‘u’, ‘r’, ‘e’ };
Note the use of single quotes around each character.
Not using single quotes around characters is an error.
Improved Initialization
n
Declare an char array to store a string
of words:
The end-of-string terminator ‘\0’
This is actually a single character, escape-zero.
Note, you need to store this symbol as the last character of your
stored string to indicate to the system where the string ends.
The consequence is that you have to factor this extra character
space requirement when declaring your array.
Do not confuse ‘\0’ with NULL, they are different!
Declaration
Initialization Techniques
The array terminator ‘\0’ special character
Usage: printf special case
Usage: scanf special case
Character array manipulation with pointers
Special Case: Array of pointers
Functions: Passing arrays to functions
Standard Library Functions for character aray
manipulation <string.h>
char Weather[ 5 ] = { ‘w’, ‘a’, ‘r’, ‘m’, ‘\0’ };
or
char Weather[ ] = { ‘w’, ‘a’, ‘r’, ‘m’, ‘\0’ };
1
Improved Initialization
Character Array manipulations
char Weather[] = "tropical";
this is equivalient to:
n
char Weather[] = { ‘t’, ‘r’, ‘o’, ‘p’, ‘i’, ‘c’, ‘a’, ‘l’, ‘\0’ };
Note: Array size is often omitted when declaring strings
(char arrays) because it becomes too invonvenient to
count the number of characters you are storing in a
sentence. However, it would be dangerous to ignore
the fact that the size of the array is the number of
characters you used in the initialization + 1 (‘\0’).
Character Array manipulations
n
To populate an array from the
keyboard: (ie. read a string from a
keyboard)
n
char Word[40];
n
scanf(“%s”, Word);
n
n
n
can store up to 39 chars + implied ‘\0’
reads characters from the keyboard and override the
contents of Word. Note: reads up to a delimiter!
Delimiter examples: space, comma, tab, newline…
Example:
buffer
0
‘s’
1
‘e’
0
‘s’
1
‘e’
n
n
char A[] = “this is a sentence”;
printf(“%s”, A);
n
n
n
output: this is a sentence
given a pointer to a char array, the printf would print the
char that the pointer is referring to as well as the rest of
the char array…. until: the ‘\0’ character is encountered.
the ‘\0’ plays a critical role in controlling or ‘stopping’ the
printing task. If you omit the ‘\0’ the printf will result in
an error (or unreadable characters being printed)
Example: Array Index
char buffer[40];
char password[] = “secret”;
int p = 0;
int match = 1;
scanf(“%s”, buffer);
while(password[p] != ‘\0’)
{
if (password[p] != buffer[p])
{
match = 0;
}
p++;
}
if (match == 1)
printf(“success”);
else
printf(“login incorrect”);
Example: Array pointers
2
3
‘c’
4
‘r’
5
‘e’
6
2
‘c’
3
4
‘r’
5
‘e’
…
‘t’ ‘\0’ ju
p 0,1,2,3,4…
password
n
printf and scanf work on char arrays slightly
differently than integer or other data type
arrays.
For example:
6
‘t’ ‘\0’
39
nk
char buffer[40];
char password[] = “secret”;
char *ptrPass = password;
char *ptrBuff = buffer;
int match = 1;
scanf(“%s”, buffer);
while(*ptrPass != ‘\0’)
{
if (*ptrPass != *ptrBuff)
{
match = 0;
}
ptrPass++; ptrBuff++;
}
if (match == 1)
printf(“success”);
else
printf(“login incorrect”);
2
Example:
Array of Pointers
ptrBuff
buffer
n
n
0
‘s’
1
‘e’
2
3
‘c’
4
‘r’
5
‘e’
6
…
‘t’ ‘\0’ ju
39
nk
n
ptrPass
password
n
0
1
‘s’
‘e’
2
‘c’
3
4
‘r’
5
‘e’
6
‘t’ ‘\0’
Storing a List of Words:
n
Season
char *Season[] = { “summer”, “fall”,
“winter”, “spring” };
0xF001
0xF008
0xF00D
‘s’
‘u’ ‘m’ ‘m’ ‘e’ ‘r’
‘f’
‘a’
‘w’ ‘i’
0xF015
‘s’
‘l’
‘l’
‘n’ ‘t’
‘p’ ‘r’
‘i’
‘\0’
‘\0’
‘e’ ‘r’
‘\0’
n
function StringLength:
1. accepts a terminated string (char array
with ‘\0’)
2. returns the size of the string in the array.
This is not the array size, this is the number
of characters that are actually used to store
the string, not including ‘\0’.
Examples:
n
n
n
char *WordList[ 10 ];
Word List Usage
printf(“%s”, Season[3]);
spring
printf(“%s”, Season[0]);
summer
int whichSeason;
scanf(“%d”, whichSeason);
printf(“%s”, Season[whichSeason]);
‘n’ ‘g’ ‘\0’
Algorithm: StringLength
n
An array that contains a list of pointers.
Each pointer refers to another array.
We can view this overall layout as a list
of ‘arrays’.
This is often used to store a list of
words.
StringLength(“hello”) à 5
StringLength(Season[1]) à 4
StringLength
int StringLength( char *A)
{
/* assume char *A ends with ‘\0’ */
int c = 0;
while(*(A+c) != ‘\0’) or while(A[c] != ‘\0’)
{
c = c + 1;
}
return c;
}
3
Common Tasks:
n
n
n
n
n
n
Computing a String Length
Comparing String contents
Copying one String to another
Appending one String to Another
Searching a String
and many more…
n
Do we need to write a function for each?
Standard Library Functions
n
n
n
n
When you include <string.h> you will have available
to you a large number functions that allow string
manipulation with ease.
First you need to write your own equivalent functions
just so you appreciate their efficiency and understand
how they work.
Once you become confident with pointers and char
arrays then you can depend on using the library
functions to speed up development time.
Caution: Library functions are written by someone
who has no knowledge of your specific application.
You need to carefully study each function along with
its assumptions and limitations in order to use it
properly as it was intended.
4
Download