cp2_4_ps07_sol

cp2_4_ps07_sol - Home Work 7 Solutions 1. Part a. Write an...

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

View Full Document Right Arrow Icon
Home Work 7 Solutions 1. Part a. Write an algorithm to check if a user entered string is a palindrome. Algorithm: Get input string and length from the user. Set Flag to True For I in 1 . . Length/2 loop If String(I) /= String(Length – I+1) then Display ‘Input_String not a Palindrome” Set Flag to False If Flag = True then Display “Input String is a Palindrome” Part b. Implement your algorithm as an Ada95 program. 1 . ----------------------------------------------- 2 . -- Program to check if a user input string is a 3 . -- palindrome 4 . -- Programmer: Joe B 5 . -- Date Last Modified : March 16, 2004 6 . ------------------------------------------------ 7 . 8 . with Ada.Text_Io; 9 . 10 . procedure Check_Palindrome is 11 . -- defines a string with maximum length of 80 12 . User_Input : String ( 1 .. 80 ); 13 . -- defines a variable to hold the length from get_line 14 . User_Length : Integer; 15 . -- flag to determine if the string was a palindrome 16 . -- remains true if the string is a palindrome 17 . Flag : Boolean := True; 18 . begin 19 . Ada.Text_Io.Put( "Please Enter a String: " ); 20 . Ada.Text_Io.Get_Line(User_Input, User_Length); 21 . 22 . for I in 1 .. User_Length/ 2 loop 23 . if User_Input(I) /= User_Input(User_Length - I+ 1 ) then 24 . Ada.Text_Io.Put_Line( "Input is not a palindrome" ); 25 . Flag := False; 26 . exit ; 27 . end if ; 28 . end loop ; 29 . if Flag = True then 30 . Ada.Text_Io.Put_Line( "Input string is a palindrome" ); 31 . end if ; 32 . end Check_Palindrome; 32 lines: No errors
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. Modify the program above to read inputs from a text file and store the reversed string in an output text file. The program should: Main Program Algorithm: Open input_file Create output_file While not end_of_file (input_file) Read input_string and length from the file. If Is_Palindrome(input_string) then Store input_string in output_file Else Reverse input_string Store reversed_string in output_file Close input_file Close output_file Reverse String Algorithm: Get Input_String and Length For I in 1 . . Length/2 Temp := Input_String(I) Input_String(I) := Input_String(Length – I + 1) Input_String(Length-I + 1) := Temp Store String Algorithm: Get Input_String and Length For I in 1 . . Length Put Input_String(I) in Output_File end loop Put new_line in Output_File Part b. Implement your algorithm as an Ada95 program. Hw_7 Package Specification Listing GNAT 3.15 p ( 20020523 ) Copyright 1992 - 2002 Free Software Foundation, Inc. 1 . ----------------------------------------------- 2 . -- package specification containing subprograms 3 . -- to reverse a string and check if the string is 4 . -- a palindrome 5 . -- Specifier : Joe B 6 . -- Date Last Modified : March 16, 2004 7 . -------------------------------------------------- 8 . with Ada.Text_Io; 9 . package Hw_7 is 10 . -- checks if a string is a palindrome, returns true if it is 11 . function Check_Palindrome (
Background image of page 2
12 . User_Input : String; 13 . User_Length : Integer ) 14 . return Boolean; 15 . -- reverses the elements in the string from 1 . . length
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.

Page1 / 11

cp2_4_ps07_sol - Home Work 7 Solutions 1. Part a. Write an...

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