08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

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

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

Chapter 8 ■ Arrays

} //end lettersOnlyLower

int palindrome(char word[]) {

int lo = 0;

int hi = strlen(word) - 1;

while (lo < hi)

if (word[lo++] != word[hi--]) return 0;

return 1;

} //end palindrome

The program prompts the user for a phrase and tells her whether or not it is a palindrome. We

also print the converted phrase to show you how the function works.

A sample run is shown here:

Type a phrase. (To stop, press "Enter" only): Madam I'm Adam

Converted to: madamimadam

is a palindrome

Type a phrase. (To stop, press "Enter" only): Flo, gin is a sin. I golf.

Converted to: floginisasinigolf

is a palindrome

Type a phrase. (To stop, press "Enter" only): Never odd or even.

Converted to: neveroddoreven

is a palindrome

Type a phrase. (To stop, press "Enter" only): Thermostat

Converted to: thermostat

is not a palindrome

Type a phrase. (To stop, press "Enter" only): Pull up if I pull up.

Converted to: pullupifipullup

is a palindrome

Type a phrase. (To stop, press "Enter" only):

8.10 Array of Strings – Name of Day Revisited

In Program P7.4, we wrote a function printDay, which printed the name of a day, given the

number of the day. We will now write a function nameOfDay that will be given two arguments:

the first is the number of a day and the second is a character array. The function will store, in the

array, the name of the day corresponding to the number of the day. For example, the call

nameOfDay(6, dayName);

will store Friday in dayName, assuming dayName is a character array.

We show how to write nameOfDay using an array to store the names of the days. Suppose we

have an array day as shown in Figure 8-2 (day[0] is not used and is not shown).

225

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!