20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Variable-Length Character Str<strong>in</strong>gs<br />

199<br />

fashion, you can elim<strong>in</strong>ate the need to specify the number of characters that are conta<strong>in</strong>ed<br />

<strong>in</strong>side a character str<strong>in</strong>g.<br />

In the C language, the special character that is used to signal the end of a str<strong>in</strong>g is<br />

known as the null character and is written as '\0'.So, the statement<br />

const char word [] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };<br />

def<strong>in</strong>es a character array called word that conta<strong>in</strong>s seven characters, the last of which is<br />

the null character. (Recall that the backslash character [\] is a special character <strong>in</strong> the C<br />

language and does not count as a separate character; therefore, '\0' represents a s<strong>in</strong>gle<br />

character <strong>in</strong> C.) The array word is depicted <strong>in</strong> Figure 10.2.<br />

word[0]<br />

word[1]<br />

word[2]<br />

word[3]<br />

word[4]<br />

word[5]<br />

word[6]<br />

'H'<br />

'e'<br />

'l'<br />

'l'<br />

'o'<br />

'!'<br />

'\0'<br />

Figure 10.2<br />

The array word with a term<strong>in</strong>at<strong>in</strong>g null character.<br />

To beg<strong>in</strong> with an illustration of how these variable-length character str<strong>in</strong>gs are used, write<br />

a function that counts the number of characters <strong>in</strong> a character str<strong>in</strong>g, as shown <strong>in</strong><br />

Program 10.2. Call the function str<strong>in</strong>gLength and have it take as its argument a character<br />

array that is term<strong>in</strong>ated by the null character.The function determ<strong>in</strong>es the number of<br />

characters <strong>in</strong> the array and returns this value back to the call<strong>in</strong>g rout<strong>in</strong>e. Def<strong>in</strong>e the<br />

number of characters <strong>in</strong> the array as the number of characters up to, but not <strong>in</strong>clud<strong>in</strong>g,<br />

the term<strong>in</strong>at<strong>in</strong>g null character. So, the function call<br />

str<strong>in</strong>gLength (characterStr<strong>in</strong>g)<br />

should return the value 3 if characterStr<strong>in</strong>g is def<strong>in</strong>ed as follows:<br />

char characterStr<strong>in</strong>g[] = { 'c', 'a', 't', '\0' };<br />

Program 10.2 Count<strong>in</strong>g the Characters <strong>in</strong> a Str<strong>in</strong>g<br />

// Function to count the number of characters <strong>in</strong> a str<strong>in</strong>g<br />

#<strong>in</strong>clude

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!