Problem Exercises Chapter 05

Problem Exercises - 1 Write a function that returns true if the string parameter is a palindrome A palindrome is any"word which is the same forward

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 DocumentRight 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
Background image of page 3

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

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

This note was uploaded on 01/27/2010 for the course DS csc 270 taught by Professor Dr.m.g during the Spring '10 term at Notre Dame University-Louaize.

Page1 / 6

Problem Exercises - 1 Write a function that returns true if the string parameter is a palindrome A palindrome is any"word which is the same forward

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online