Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions The goal of this assignment is to understand the runtime and memory usage of common commands. You are provided with an incomplete C program.

Instructions

The goal of this assignment is to understand the runtime and memory usage of common commands.

You are provided with an incomplete C program. It uses five string functions. These are functions that are built into almost every high level language. You are going to implement them yourself. Then you are going to determine the runtime and memory usage of each function.

The primary goal of this assignment is to understand that as programmers it is important to know what built in tools do. The runtime of code we write is dependent on the code we use. If you dont have an idea of what that code is doing, you cannot estimate the complexity of your algorithms.

Provided Code

You are provided with three files. You only need to make changes to one file. The others includes important instructions. Please review all provided files.

main.c - a collection of tests. You may not change this file

stringTools.h - prototypes for the functions assigned on the homework. You may not change this file. You must match the provided prototypes.

stringTools.c - the function definitions. You must implement the functions defined here. This is also where you will put your analysis.

Replit will compile automatically. If you want to compile it manually the command would be:

 gcc -o main main.c stringTools.c 

Then you would excute it by typing ./main

Note that you only list the c files and not the h files in the compile command.

Functions

The functions you are doing to define are described in stringTools.h. This is just a summary.

int length(char* str); - Determine the length of the string

char* upper(char* str); - Convert all characters in str to uppercase

int find(char* str, char target); - Find the position of the first appearance of target in str

char* replace(char* str, char oldChar, char newChar); - Replace all appearances of oldChar with newChar in str

bool lessThan(char* str1, char* str2); - Determine str1 < str2

These are functions that are commonly provided in other languages. Here are examples in Python and Java showing the equivalent commands.

Python Example

someString = "This is a test string for showing features." #Length of a string print("Length is",len(someString)) #Convert to UpperCase newVer = someString.upper() print("Upper Case:",newVer) #Find Index of a Value res = someString.find("a") print("Location of a is",res) #Replace a letter newRep = someString.replace("t","!") print("After Replace:",newRep) #Less Than A = "Cat" B = "Cats" print("Less Than Test 1:",A < B) print("Less Than Test 2:",B < A) 

Result of Running this code:

 Length is 43 Upper Case: THIS IS A TEST STRING FOR SHOWING FEATURES. Location of a is 8 After Replace: This is a !es! s!ring for showing fea!ures. Less Than Test 1: True Less Than Test 2: False 

Java Example

class example{ public static void main(String args[]){ String someString = "This is a test string for showing features."; //Length of a string System.out.printf("Length is %d ",someString.length()); //Convert to UpperCase String newVer = someString.toUpperCase(); System.out.printf("Upper Case: %s ",newVer); //Find Index of a Value int res = someString.indexOf("a"); System.out.printf("Location of a is %d ",res); //Replace a letter String newRep = someString.replace('t','!'); System.out.printf("After Replace: %s ",newRep); //Less Than String A = "Cat"; String B = "Cats"; System.out.printf("Less Than Test 1: %d ",A.compareTo(B)); System.out.printf("Less Than Test 2: %d ",B.compareTo(A)); return; } } 

Result of Running this code:

 Length is 43 Upper Case: THIS IS A TEST STRING FOR SHOWING FEATURES. Location of a is 8 After Replace: This is a !es! s!ring for showing fea!ures. Less Than Test 1: -1 Less Than Test 2: 1 

Analysis

One you implement each function, you must do an analysis of the code you wrote.

For memory usage, determine the number of bytes uses for different parts of the function. You must determine the following:

The number of bytes taken by all function inputs.

The number of bytes in the return value.

The number of bytes used to store local variables.

The number of bytes allocated on the heap.

For the runtime analysis, put the number of times each operation is performed during execution. A Table template is provided in the files.

 | Operation | Count | | --------- | ----- | | ? | ? | 

Add as many rows as you need to write all the operations you used in the functions.

Examples

Example 1

 Welcome to String Tool Tester. Select Function to Test: 1.) length 2.) upper 3.) find 4.) replace 5.) lessThan 1 Testing length Enter A String: Hats Input Was: Hats Length Was: 4 

Example 2

 Welcome to String Tool Tester. Select Function to Test: 1.) length 2.) upper 3.) find 4.) replace 5.) lessThan 2 Testing upper Enter A String: Cats are nice pets. Input Was: Cats are nice pets. After Function Input Was: Cats are nice pets. Return value Was: CATS ARE NICE PETS. 

Example 3

 Welcome to String Tool Tester. Select Function to Test: 1.) length 2.) upper 3.) find 4.) replace 5.) lessThan 3 Testing find Enter A String: I am a long string with different words. Enter Character to find: r First Input Was: I am a long string with different words. Second Input was: r Return value Was: 14 

Example 4

 Welcome to String Tool Tester. Select Function to Test: 1.) length 2.) upper 3.) find 4.) replace 5.) lessThan 4 Testing replace Enter A String: This string has some letters in it. Enter Character to find: t Enter Character to replace with: Q Input String: This string has some letters in it. Find Char: t Replace Char: Q After Function Input Was: This string has some letters in it. Return value Was: This sQring has some leQQers in iQ. 

Example 5

 Welcome to String Tool Tester. Select Function to Test: 1.) length 2.) upper 3.) find 4.) replace 5.) lessThan 5 Testing lessThan Enter first String: Cat Enter second String: Cats Input String 1: Cat Input String 2: Cats Return value Was: 1 

Q- There is a project in Replit for this assignment. You need three files: main.c, stringTools.c, and stringTools.h

Q- Optional: readme.txt if you have anything to tell the grader before they run your assignment.

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

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions