20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

232 Chapter 10 Character Str<strong>in</strong>gs<br />

to be <strong>in</strong>serted, and the position <strong>in</strong> the source str<strong>in</strong>g where the str<strong>in</strong>g is to be<br />

<strong>in</strong>serted. So, the call<br />

<strong>in</strong>sertStr<strong>in</strong>g (text, "per", 10);<br />

with text as orig<strong>in</strong>ally def<strong>in</strong>ed <strong>in</strong> the previous exercise, results <strong>in</strong> the character<br />

str<strong>in</strong>g "per" be<strong>in</strong>g <strong>in</strong>serted <strong>in</strong>side text, beg<strong>in</strong>n<strong>in</strong>g at text[10].Therefore, the<br />

character str<strong>in</strong>g "the wrong person" is stored <strong>in</strong>side the text array after the<br />

function returned.<br />

8. Us<strong>in</strong>g the f<strong>in</strong>dStr<strong>in</strong>g, removeStr<strong>in</strong>g,and <strong>in</strong>sertStr<strong>in</strong>g functions from preced<strong>in</strong>g<br />

exercises, write a function called replaceStr<strong>in</strong>g that takes three character<br />

str<strong>in</strong>g arguments as follows<br />

replaceStr<strong>in</strong>g (source, s1, s2);<br />

and that replaces s1 <strong>in</strong>side source with the character str<strong>in</strong>g s2.The function<br />

should call the f<strong>in</strong>dStr<strong>in</strong>g function to locate s1 <strong>in</strong>side source, then call the<br />

removeStr<strong>in</strong>g function to remove s1 from source, and f<strong>in</strong>ally call the<br />

<strong>in</strong>sertStr<strong>in</strong>g function to <strong>in</strong>sert s2 <strong>in</strong>to source at the proper location.<br />

So, the function call<br />

replaceStr<strong>in</strong>g (text, "1", "one");<br />

replaces the first occurrence of the character str<strong>in</strong>g "1" <strong>in</strong>side the character str<strong>in</strong>g<br />

text, if it exists, with the str<strong>in</strong>g "one". Similarly, the function call<br />

replaceStr<strong>in</strong>g (text, "*", "");<br />

has the effect of remov<strong>in</strong>g the first asterisk <strong>in</strong>side the text array because the<br />

replacement str<strong>in</strong>g is the null str<strong>in</strong>g.<br />

9. You can extend even further the usefulness of the replaceStr<strong>in</strong>g function from<br />

the preced<strong>in</strong>g exercise if you have it return a value that <strong>in</strong>dicates whether the<br />

replacement succeeded, which means that the str<strong>in</strong>g to be replaced was found<br />

<strong>in</strong>side the source str<strong>in</strong>g. So, if the function returns true if the replacement succeeds<br />

and false if it does not, the loop<br />

do<br />

stillFound = replaceStr<strong>in</strong>g (text, " ", "");<br />

while ( stillFound = true );<br />

could be used to remove all blank spaces from text, for example.<br />

Incorporate this change <strong>in</strong>to the replaceStr<strong>in</strong>gs function and try it with various<br />

character str<strong>in</strong>gs to ensure that it works properly.<br />

10. Write a function called dictionarySort that sorts a dictionary, as def<strong>in</strong>ed <strong>in</strong><br />

Programs 10.9 and 10.10, <strong>in</strong>to alphabetical order.<br />

11. Extend the strToInt function from Program 10.11 so that if the first character of<br />

the str<strong>in</strong>g is a m<strong>in</strong>us sign, the value that follows is taken as a negative number.

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

Saved successfully!

Ooh no, something went wrong!