{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Problem Exercises Chapter 05 - 1 Write a function that...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1. Write a function that returns true if the string parameter is a palindrome . A palindrome is any "word" which is the same forward and backward. Example: "radar", "noon", "20011002", ... The function should return false if the argument is not a palindrome. Assume the prototype for function palindrome is: bool IsPalindrome( char *s1); // function prototype Answer : bool IsPalindrome( char *s1) { char *s2; int len; len = strlen(s1); s2 = s1; while (*s2 != '\0') { s2++; } s2--; for ( int i = 0; i < len/2; i++) { if (*s2 != *s1) { return false ; } s1++; s2--; } return true ; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2. Write a function which shortens a string " s1 " to " n " characters and stores the result in string " s2 ". Assume the prototype is: // function prototype void truncate( char *s1, char *s2, int n); Answer : void truncate( char *s1, char *s2, int n) { for ( int i = 0; i < n; i++) { *s2 = *s1; s1++; s2++; } *s2 = '\0'; }
Background image of page 2
3. Write a program that encodes English language phrases into pig Latin. To translate each English word into a pig-Latin word, place the first letter of the English word at the end of the English word, and add the letters "ay". Thus the word "jump" becomes "umpjay", and the word "the" becomes "hetay".
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}