COMPSCI 210 Part II A4 Help Part 1 Write a C function to print out a POSTIVE integer value in base 4. (unsigned representation) • Steps: • • Read a number: • Use _________ Write a method to print the number in base 4. • Use a Loop to work out the value • Example: 26 -> 122 • 26/4 => remainder: 2 • 6/4 => remainder: 2 • 1 (answer = 122) II-13-2 Part 1 Using redirection • • • • Input file: only contain ONE positive number Output: the number in base 4 You don’t need to change your source code. Just run the code: • Q1 < number1.txt • Q1 < number2.txt II-13-3 Part 2 A Write a C function that • takes a parameter a character string of unknown length, containing a single word • translate this string from English into Pig Latin. • move the first letter of the string, and append it onto the end • concatenate the letters “ay” • Assume that the array contains enough space for you to add the extra characters. char* toPLString(char * inputstring); • Examples: • Good -> oodGay • Bad -> adBay II-13-4 char* toPLString(char * inputstring); Steps: • Input: • • • o o d \0 o o d G a the original string (pointer) Output: • G y \0 The pointer Task: • • • • • • • • • Store the _______char Copy the second char to the first char position and so on Until the end of the string (______character) Go back to the _______character Put the _____character Put 'a' Put 'y' Put the _____character Return the pointer II-13-5 Part 2 B Write a program that reads a string from the keyboard and displays it in Pig Latin •Steps: • Main method with arguments • Number of arguments >=___ (_______, option) • Use switch statement (note: must be integral type: char) Or if … • Use atoi to change ASCII to Integer • Assumption: • size of the array = 7 including ay\0, total you can read = MAX =4 • Text file only contain ONE line and one word • Word with no more than 8 characters II-13-6 Case 1 Using getChar + while loop • Input: the array to store the string • Steps: • Read each char using the getChar method and store the character in the string • Does it contain the newline character? • Stop condition: \n or = ______ • Put the ____char at the end • Output: return number of characters read • Cases: • Bad\n=> Bad\0 • Good\n => Good\0 • Hello\n => Hell\0 input string II-13-7 Case 2 Using fgets • Input: the array to store the string • Steps: • how many characters should you read? MAX=4 • Use fgets(buffer, _______, stdin) to read a string • Does it contain newline character? • Output: return number of characters read • Cases: • Bad\n=> Bad\n => Bad\0 • Good\n => Good => Good\0 • Hello\n => Hell => Hell\0 input fgets string II-13-8 Case 3 Using scanf • Input: the array to store the string • Steps: • Use scanf to read a string • Does it contain newline character? • May overflow! • Size of the array = 7, • input: 8 chars -> CRASH • Input <8 chars -> OK, but may overwrite values • Take MAX characters only, set buffer[_____] to the null char • Output: return number of characters read • Cases: • Bad\n=> => Bad\0 • Good\n => Good\0 • Hello\n => Hell\0 II-13-9 Part 2 Using redirection • • • • Input file: only contain ONE word and no more than 8 characters Output: the string in Pig Latin You don’t need to change your source code. Just run the code: • Q2 1 < input1.txt • Q2 2 < input1.txt • Q2 3 < input1.txt II-13-10 Part 3 required to read all words from a text file and translate them from English into Pig Latin. • Assumption • contains multiple lines • one word per line. • Size limit of each word to 128 • Steps: • Declare a file pointer and open the text file • Read a line at a time until the _____of the file • Convert each word • Display the answer • close the file • Examples: • Q3 finput1.txt • Q3 finput2.txt II-13-11
© Copyright 2025 Paperzz