Practice Problem 2.26 (solution page 151)
You are given the assignment of writing a function that determines whether one string is longer than another. You decide to make use of the string library function strlen having the following declaration:
/* Prototype for library function strlen */
size_t strlen(const char *s);
Here is your first attempt at the function:
/* Determine whether string s is longer than string t */
/* WARNING: This function is buggy */
int strlonger(char *s, char *t) {
return strlen (s) - strlen (t) > 0;
}
When you test this on some sample data, things do not seem to work quite right. You investigate further and determine that, when compiled as a 32-bit
program, data type size is defined (via typed) in header file stdio . h to ̑ be unsigned.
- A. For what cases will this function produce an incorrect result?
- B. Explain how this incorrect result comes about.
- C. Show how to fix the code so that it will work reliably.
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
COMPUTER SYSTEMS&MOD MSGT/ET SA AC PKG
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (7th Edition)
Starting Out With Visual Basic (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
Starting Out with C++: Early Objects
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
- I need help with this C++ problemarrow_forwardProblem 4 (Searching a list of Student objects)Write a function called find_by_ID(students, id) that takes a list of student objects(students) and an int (id) as parameters, and returns the student object in the list withthe given id. If there is no student with the given id, it returns “NA”.See the sample output below. Looking for a student with ID = 123: Student: Alice, ID# 123 found Looking for a student with ID = 999: Student: Cathy, ID# 567 foundLooking for a student with ID = 555: NA foundarrow_forward6) The second moment of inertia is a mathematical representation of a beam's resistance to bending. a. Write a function that returns the second moment of inertia (1) for a rectangular section which has a width b and height h, as bh shown in the figure. The formula is I = : following function prototype: Use the 12 Def rectangleMomlnertia(b,h):arrow_forward
- the following 12. (Greatest Common Divisor) Given two integers x and recursive definition determines the greatest common divisor of x and y, written gcd(x,y): Y, if y = 0 gcd(x, y) = %3D gcd(y, x%y) if yチ0 Note: In this definition, % is the mod operator. write a recursive function, gcd, that takes as parameters two integers and returns the greatest common divisor of the numbers. Also, write a pro- gram to test your function.arrow_forwarduse C programming and use comments.arrow_forwardC Programming "Functions"arrow_forward
- 2arrow_forwardTask - Encode a string (C Language) Modify Project #2, Task #1 (given below) so that input characters are command line arguments. Requirements Name your program project5_encode.c. Input characters are command line arguments. There can be any number of command line arguments. Assume the total number of input characters is no more than 1000. Character handling library functions in ctype.h are allowed. The program should include the following function: void encode(char *input, char *output); The function expects input to point to a string containing the string to be encoded, output to point to a string containing the result. The program should also check if the number of arguments on the command line is greater than or equal to 2. If the number of arguments is 1, the program should display the message "Invalid input!". Examples (your program must follow this format precisely) Example #1 $ ./a.out 7 + 8Output: 3_4 Example #2 $ ./a.out usf.eduOutput: ayl_kja Example #3 $…arrow_forwardPython language I have one and half hourarrow_forward
- 1.Objective Write and debug a program to deepen the understanding of function prototype, definition and call. 2.Content (Parking Charges) A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $0.50 per hour over three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate and print the parking charges for each of three customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a tabular format, and should calculate and print the total of yesterday's receipts. The program should use the function calculateCharges to determine the charge for each customer. Your outputs should appear in the following format: 3.Flowchart of the Algorithm 4.Codes 5.Tests and Results 6.Summary //…arrow_forwardProblem statementWrite a program that receives a value n and prints the expansion of the binomial (x + y) n. To do this, define the following auxiliary functions: - Define a function that calculates the binomial coefficient of two numbers. - Define a function that receives a character representing a variable and a positive integer value, n, and returns a string representing the variable raised to the power n. EntryA positive integer. Which will meet the following condition 1≤n≤25. DepartureA character string representing the expansion of the binomial (x + y) n. The string must be written in pythonic style. It should end with a line change. Examples Input Example 1 4Output Example 1 x ** 4 + 4 * x ** 3 * y + 6 * x ** 2 * y ** 2 + 4 * x * y ** 3 + y ** 4 Input Example 2 3Output Example 2 x ** 3 + 3 * x ** 2 * y + 3 * x * y ** 2 + y ** 3arrow_forwardIn C++, thank you!arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education