an array type for strings. two ways to represent strings – i.e. “hello” cstring an array with...

12
An Array Type For Strings

Upload: augustine-butler

Post on 24-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

An Array Type For Strings

Page 2: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

Two ways to represent strings – i.e. “Hello”

cstring• An array with base type

char• Older way of processing

strings• Null character marks end of

string

string• New way to process string

types• Discussed next section

Page 3: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

A closer look at cstring

• An array of characters• “Hello” stored as six indexed variables, ‘h’, ‘e’, ‘l’, ‘l’,

‘o’, and the null character• Null character used as an end marker, distinct from

all ‘real’ characters– ‘\0’– Is a single character, just like tab or newline

• How many characters is a c-string variable capable of holding if it is declared as:– char s[10];

Page 4: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

Partially filled c-string variables

• Does not use an int variable to keep track of the number of slots filled

• Uses the null character to mark the last character in the c-string

• Draw: “Hi Mom!” if in the array char s[10];char s[10] = “Hi Mom!”;

• General declaration: char my_c_string[maxsize + 1]why +1?

Page 5: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

Initializations• Example 1:

char myMessage[20] = “Hi there.”;• Example 2:

char shortString[] = “abc”;• Example 3:

char shortString[4] = “abc”;• Example 4:

char shortString[] = {‘a’, ‘b’, ‘c’};• Are examples 2 and 4 equivalent? Why/why not?• Initializing like we did in examples 1, 2, and 3 automatically places the null

character in the array at the end of the specified characters• If the size is omitted (like in example 2), then the c-string variable will be

given a size one character longer than the length of the c-string

Page 6: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

• Consider the declaration:• char ourString[5] = “Hi”;• Indexed Variables:

– ourString[0]– ourString[1]– ourString[2]– ourString[3]– ourString[4]

• Write the code that will change ourString to a c-string of the same length consisting of all ‘X’ characters– You should be careful not to replace the null character– Many string manipulating functions depend on the presence of the null

character

Page 7: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

Using = and == with C strings• You can’t use the assignment operator with c-strings as you would with normal variables

– Assignment is different from initialization (which is legal)– Ex: char aString[10]; aString = “Hello”; //illegal!

• The easiest way to assign a value to a c-string variable is to use the strcpy function– Ex: strcpy(aString, “Hello”);– Be careful – this function does not check that the value assigned is within the appropriate length

• A safer version: strncpy– Takes a third argument that gibes the max number of characters to copy– Ex: char anotherString[10]; strncopy(anotherString, aStringVariable, 9);

• Instead of using == to test equality of c-string variables, use the function strcmp– Ex: if(strcmp(cString1, cString2))

cout << “strings are not the same” << endl; else cout<< “Strings are the same” << endl;

Page 8: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

The <cstring> library

• Not needed to declare/initialize c-string variables

• Needed for many predefined functions needed to process c-string variables

Page 9: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

Function Description Cautionsstrcpy(target, source) Copies c-string value source into

the c-string variable targetDoes not check to make sure target is large enough to hold value SrcString

strncpy(target, source, limit) The same as the two-argument strcpy except that at most limit characters are copied

If limit is chosen carefully, this is safer than strcpy. Not implemented in all versions of C++

strcat(target, source) Concatenates the c-string value source onto the end of the c-string in target

Does not check to see that target is large enough to hold the result of the concatenation

strncat(target, source, limit) The same as the two argument strcat except that at most limit characters are appended

Not implemented in all versions of C++

strlen(source) Returns an integer equal to the length of source. The null character is not counted

strcmp(string1, string2) Returns 0 (false) if the strings are the same, returns a value < 0 if string1 is “less than” string2, returns a value > 0 if string1 is “more than” string2 (lexicographic order)

strncmp(string1, string2, limit) The same as the strcmp except at most limit characters are compared

Page 10: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

C-String Arguments and Parameters

• Keep in mind that a c-string is an array, so it behaves as such as a function parameter

• Safest to include an int parameter giving the declared size of the c-string variable

Page 11: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

Input and Output

• Can use << for output• Can use >> for input, but keep in mind how it

behaves with white space– Ex 1 in code

• Can also use the getline() function for input– Accepts two arguments, the first the c-string

variable, the second the size of the c-string– Ex 2 in code

Page 12: An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character

C-String-to-Number conversionsFunction Return Type Pronunciation

atoi int A – to – I

atol long A – to – L

atof double A – to - F

• All functions accept a c-string argument

• See ex. 3 in code