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
EBK COMPUTER SYSTEMS
Additional Engineering Textbook Solutions
Degarmo's Materials And Processes In Manufacturing
Management Information Systems: Managing The Digital Firm (16th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
- 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_forwardC Programming "Functions"arrow_forwardQ2) Write down a C/C++ function that increases the salary of the given employee ID by 20 percent. Multiply the salary with 1.2 to give the increase. Linear search algorithm can be used to find the given ID. struct employee{ int ID; char class; int salary; }a [100]; prototype: lovee (struct employee a [], int size, int searchID);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_forwardbcbarrow_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_forwardc++arrow_forwardC++ Programming. Topic: Working with pointers and dynamic memory. Indicators. Working with dynamic memory. Dynamic arrays and their use as function parameters. Task : Describe a void function named Swap(x,y) that swaps the values stored in the variables x and (x is a real type parameter and is both input and output). Using this function , for the given variables of real type a, b, c, d, one should sequentially replace the values of the pairs (a, b), (c, d) and (b, c) and let a, b, c, d be new values .arrow_forward
- Problem 7.1: Phinding Palindromic Phrases Purpose: Learn string manipulation in the context of a simple program. For this problem, you will modify an existing program to be more flexible. Go to your copy of CheckPalindromicPhrase.ipynb from Lab Assignment 7. In the flavor of the work begun there, continue to modify the is_pal_phrase function to work for palindromic phrases and sentences by having it ignore spaces ignore case (upper versus lower) ignore common punctuation, that is the thirteen characters , ; : - ' " . ? ! ( ) [ ] For example, "a Toyota" and "Madam, I'm Adam." and "Was it a rat I saw?" should be identified as palindromes; ditto for "A man, a plan, a canal--Panama!" and "Madam (in Eden), I'm Adam." Comment every line you added, using the following format: # YOURINITIALS: your comment For example, here is a line from my function using one of the lines suggested in LA7: s = s.replace(" ","") # MSB: remove spaces from string A good plan of attack would be to add a…arrow_forwardC++ Programming point* go_far(point* other) Return the point bearing the x- and the y- coordinates of whichever x- and y-coordinates is further from the origin. Identify which of the two points have the furthest x- and y-coordinates independently. Example: this point has (-5, 1) and the other point has (4, 9). In the x-coordinate of the two points, -5 is further from the origin than 4. In the y-coordinate of the two points, 9 is further from the origin than 1. Hence, we return a new point bearing the coordinates (-5, 9).arrow_forwardC++ Problem 1: (Monte Carlo Experiments) The Monte Carlo method is used in modeling a wide-range of physical systems at the forefront of scientific research today. Let’s consider the problem of estimating the area of the region x 2 + 2y 2 ≤ 1 by utilizing the Monte Carlo method. This region is enclosed by a blue ellipse, which is inscribed in a 2 × 2 red square (shown in the figure below). The experiment simply consists of throwing darts on this figure completely at random (meaning that every point in the red square has an equal chance of being hit by the dart). You keep throwing darts at random. All of the darts fall within the square, but not all of them fall within the ellipse. If you throw darts completely at random, this experiment estimates the ratio of the area of the ellipse to the area of the square, by counting the number of darts within each area. Program this Monte Carlo method to compute the area of the blue ellipse using different numbers of darts. For each experiment,…arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr