Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Assignment 1. You are given a C file hw04q1.c which contains a partially completed program. Follow the instructions contained in comments and complete the

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

Programming Assignment 1. You are given a C file hw04q1.c which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be writing most functions (initializeStrings, printStrings, toUppercase, tolowercase, changeAllToUpperOrlower, wordMatch, concatStr) using only pointer operations instead of using array operations. In this homework, the arrays in all functions need to be accessed with pointer, not as array. It means you cannot use an array element value through indexing like s[0][0] or s[i]j]. However, you can use the addresses of some array elements, such as &s[0][0] and &s[i][0]. Then, you use the pointer to access the array element value. You will be using a character pointer. You may use only the strlen() function from string.h library. You can find out more about these functions by reading through the instructions in the hw04q1.c file. Example output is given below. CSE240 HW4: Pointers Enter a string: Arizona Enter a string: sunsets Enter a string: are Enter a string: amazing Enter a string to find a match: Sun Match not found The concatenated string is: Arizona sunsets are amazing Change strings to a particular case: arizona sunsets are amazing // Enter your name here // State the IDE that you use: Visual Studio or GCC #include #include #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. // You can assume that all inputs are valid. Ex: If prompted for an integer, the user will input an integer. // You can use only the strlen() of strings.h library to check string length. Do not use any other string functions // because you are supposed to use pointers for this homework. // **** DO NOT use arrays to store or to index the characters in the string **** // Global Macro Values. They are used to define the size of 2D array of characters #define NUM_STRINGS 4 #define STRING_LENGTH 50 // Forward Declarations void initializestrings (char[NUM_STRINGS] [STRING_LENGTH]); void printstrings (char[NUM_STRINGS] [STRING_LENGTH]); void toUppercase (char[STRING_LENGTH]); void tolowercase(char[STRING_LENGTH]); void changeAllToUpperorLower(char[NUM_STRINGS][STRING_LENGTH], char); void wordMatch(char strings [NUM_STRINGS][STRING_LENGTH], char input[STRING_LENGTH]); void concatstr(char[NUM_STRINGS][STRING_LENGTH], char[NUM_STRINGS * STRING_LENGTH]); // Problem 1: initializestrings (5 points) // Use pointer 'p' to traverse the 2D array of characters variable 'strings' (input from user in main() ) and set all characters in each // array to a null terminator so that there is a 4 row and 50 column 2D array full of null terminators. // The null terminator 'lo' is used to denote the end of a string. void initializestrings (char strings [NUM_STRINGS][STRING_LENGTH]) { char* p = &strings[0][0]; // enter code here } // Problem 2: printstrings (5 points) // Use pointer 'p' to traverse the 2D character array 'strings' and print each string. // See the example outputs provided in the word document. Each string should be printed on a new line. void printstrings (char strings [NUM_STRINGS][STRING_LENGTH]) { char* p = &strings[0][0]; // enter code here } // Problem 3: touppercase) and to Lowercase) (15 points) // Problem 3-1: touppercase // Convert the string in the array 's' to an uppercase string. // Traverse the input using pointer 'p' and covert the character to an uppercase letter if it is lowercase. // You need to write down the code to check whether or not the character is lowercase. // HINT: Use a pointer to change the ASCII value of a character. // The ASCII value of 'a'= 97 and the ASCII value of 'A' = 65 so the difference between them is 32. void toUppercase(char s[STRING_LENGTH]) { char* p = &s[0]; // enter code here } // Problem 3-2: toLowercase // HINT : This should be very similar to touppercase(). // Convert the string in the array 's' to an lowercase string. // Traverse the input using pointer 'p' and covert the character to a lowercase letter if it is uppercase. // You need to write down the code to check whether or not the character is uppercase. // HINT: Use a pointer to change the ASCII value of a character. The ASCII value of 'a'= 97 and the ASCII value of 'A' = 65 so the difference between them is 32. void tolowercase(char s[STRING_LENGTH]) { char* p = &s[0]; // enter code here // Problem 4: changeAllToUpperorLower (5 points) 1/ Change all words in the array 'strings' to uppercase or lowercase based on the value of char variable 'set'. // If the value of char 'set' is 'u' or 'u', change all words to uppercase strings. // If the value of char 'set' is 'l' or 'l', change all words to lowercase strings. // No change, otherwise. // You may use touppercase() and to Lowercase) here. void changeAllToUpperorLower(char strings [NUM_STRINGS][STRING_LENGTH], char set) { char* ptr = &strings[0][0]; // enter code here } // Problem 5: wordMatch (10 points) // This function must be working as follows: // 1. Compare the strings in the 2D array with input string // 2. If one of them matches with input string, print "Match found" 1/ 3. Else print "Match not found" // Example: strings array -> ["Hi", "Sun", "Devils"], input -> "Sun" => print "Match found" // Example: strings array -> ["Hi", "Sun", "Devils"], input -> "Hello" => print "Match not found" void wordMatch(char strings [NUM_STRINGS] [STRING_LENGTH], char input[STRING_LENGTH]) { char* p = &strings[0][0]; char* ip = input; int flag = 0; // enter code here } // Problem 6: concatstr (10 points) // This function must be working as follows: // 1. Concatenate the string in the array 'strings' together in a sentence. // 2. Store the string to the array 'result'. // 3. Print the string in the array 'result'. // The sample output is shown in document // NOTE: This is the function partially containing what you have implemented so far. // Initialize the array 'result' first before you store a string to it. // You may declare and use more pointers if needed. void concatstr(char strings [NUM_STRINGS][STRING_LENGTH], char result[NUM_STRINGS * STRING_LENGTH]) { char* p_result = result; char* p_input = &strings[0][0]; // enter code here // You should study and understand how main() works. // *** DO NOT modify it in any way *** int main() { char strings [NUM_STRINGS ] [STRING_LENGTH]; // will store four strings each with a max length of 34 int i, key; char input[STRING_LENGTH]; char result[NUM_STRINGS * STRING_LENGTH]; printf("CSE240 HW4: Pointers "); initializeStrings (strings); for (i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Decisions Based On Data Analytics For Business Excellence

Authors: Bastian Weber

1st Edition

9358681683, 978-9358681680

More Books

Students also viewed these Databases questions

Question

What is Ramayana, who is its creator, why was Ramayana written?

Answered: 1 week ago

Question

To solve by the graphical methods 2x +3y = 9 9x - 8y = 10

Answered: 1 week ago