$30
Important Note: when writing your own copy of library functions:
1. You should only use pointer-notation. It means you are not supposed to use any square brackets, [], in your functions. In other words, your function must only use pointer arithmetic.
2. You are not allowed to call any library functions in your version of functions.
From D2L download the file lab4exD.c. Study the file and write down your prediction for the program output. Compile the program and run it to check your prediction. If your prediction was wrong, try to understand why.
Make another copy of lab4exD.c; call the copy my_lab4exD.c. In my_lab4exD.c, add a function definition for my_strlen that uses pointer arithmetic (pointer – pointer) to calculate the length of a c-string. Then, replace all of the calls to strlen in the function main with calls to my_strlen, and then make sure your modified program produces appropriate output.
Once my_strlen is working, add a function definition for my_strncat to my_lab4exD.c. Don't make any function calls within your definition of my_strncat. Replace all of the calls to strncat in main with calls to my_strncat.
Exercise E: Reading Numeric Input as a String Read This First:
The scanf library function may not be always the best tool to read the user’s numeric input. An alternative method is to read the user's input as a string of characters, and then, after a complete input errorchecking, to convert it to a number. Consider a string of digits such as:
char string[4] = "237";
'2'
''
'7'
'\0'
The above picture shows each digit is stored in one of the elements of the array string. The ASCII values for each of these characters are:
'2' == 50
'3' == 51
'7' == 55
The following calculation can be used to convert this string digits to the integer value 237. Here is the method of calculation
int num;
num = (string[0]–'0')*100 + (string[1]–'0’)*10
+ (string[2]–'0');
Note: You need to subtract the character code '0', or its ASCII value, 48, from each character in the string to get its actual value (a digit from 0 to 9). (Also, the above only works for three-digit numbers! Something smarter is needed to handle a digit sequence with a length that isn't known in advance.)
Read This Second:
A preferred function to read strings in C is the library function fgets. The following example shows how to use this function
char string [6]; printf("Enter a string: "); fgets(string, 6, stdin);
e above statements reads a string of up to maximum five characters or up to the end-of-line (‘\n’ which is Enter on most keyboards), whichever comes first.
stdin as the third argument indicates that the input comes from the terminal. (A different third argument of type FILE* could be used to read a string from a file; more about that later in the course.) For example if user enters: "ABCDEFGH" it only stores the "ABCDE" followed by a '\0’. See the following picture:
'A'
'B'
'C'
'D'
'E'
'\0'
If user’s input is "AB", it reads up to and including the '\n' (Enter).
'A'
'B'
'\n'
??
??
??
In this case, you may have to write a couple of lines of code to remove the '\n'. This means to replace it with a '\0', if it is not desired that '\n' be part of the input.
What to Do: Download files read_input.c, read_input.h, read_int.c, and prog_one.c from D2L, and take the following steps:
Step one: Compile, the program and run the program. When asked to enter integer numbers, enter valid inputs (positive or negative integers). The program converts your input from a string of digits to an integer and displays the value.
Step two: Now enter several non-integer input such as:
12abc
12..9 23.4 .56
-0.45
The program indicates your input is “Invalid”.
Step three: Read the source code and try to understand how the program works.
Step four: Modify the program to be able to read real numbers such as:
23.4
.56
-.23
-0.45
-0.0000067
(It does not have to handle input in scientific notations, such as 1.23e+45, but it should accept integers as real values.)
Note: the program should still detect invalid (non-numeric) input such as:
12abc
12..9 123.4bb bb123.4 $1235.99
To implement this step you should create a file called read_double.c that contains the following functions. (See also the function interface comments in the header file, read_input.h):
nt read_real(char* digits, int n, double * num); int is_valid_double(const char* digits); double convert_to_double(const char *digits);
You should also create a file called prog_two.c, similar to prog_one.c that demonstrates your functions in the read_double.c work.
This is an exercise about working with strings, so we are NOT allowing you to call any library functions from your convert_to_double function.
23.4 .56
-.23
-0.45
-0.0000067
564469999
+8773469
+.5
Also, you program should detect any invalid input such as:
• Too many decimal points (for example “12..999”).
• Invalid character(s) (for example “23avb45”, or 2,347).
• Invalid space(s) between digits (for example “+ 234 77”).