Answer & Explanation:Check the PDFlab3__1_.pdf
lab3__1_.pdf
Unformatted Attachment Preview
ECEC 304: Design with Microcontrollers
Lab 3: Strings and Arrays
Instructor: Prof. Naga Kandasamy
ECE Department, Drexel University
January 22, 2016
This lab assignment comprises four problems dealing with string and array manipulation. Once
you have successfully completed each part, please show the output of the program to the teaching
assistant to be checked for correctness. Complete the lab by the beginning of your lab sections
during the week of February 1. Your C code must be clearly written, properly formatted, and well
commented for full credit. Do not use any in-built string handling functions supplied by the standard C library such as strcmp, strstr, strlen, etc. Use pointers whenever possible.
You may work on this assignment in a team of up to two people. Also, note that the submitted
solutions must be the original work of your team. Solutions copied from other teams or from
online sources will result in a grade of zero for the entire lab assignment.
Important Submission Instructions: In addition to getting your work checked off by the TAs,
upload the following items to BBLearn: (1) a file called validateLuhn.c containing only the
code for the Luhn checksum validation algorithm for Lab 3c; and (2) a file checkPalindrome.c
containing only the code for the function for Lab 3d; You can find the submission links on BBLearn
under the Lab submissions/Lab 3 link. One submission by a team is sufficient.
1
Lab 3a: String Comparison
(15 points) Write functions which:
1. Accept two strings as inputs from the terminal and compare them for equality. If they are
equal, 0 is returned, otherwise the difference in value between the first two non-matching
characters is displayed on the terminal.
2. Accept a string and a character as inputs from the terminal. Find the first occurrence of a
specific character in a given string. Display the location of the occurrence within the string,
or -1 if it is not found.
3. Take two strings as arguments. If the first exists in the second as a substring, display the
starting location of the first occurrence, otherwise -1. The terminating ’ ’ characters are
not compared.
You may use the following code skeleton as a starting point for writing your code.
int compareStrings(char *str1, char *str2)
{
/* Your code goes here. */
}
int searchForChar(char *str, uint32 ch)
{
/* Your code goes here. */
}
int searchSubString(char *str, char *subStr)
{
/* Your code goes here. */
}
int main()
{
CyGlobalIntEnable;
UART_1_Start();
/* Enable global interrupts. */
/* Start the UART component. */
int choice;
int8 status;
uint32 ch;
char str1[MAX_BUFFER_SIZE], str2[MAX_BUFFER_SIZE];
while(1){
writeString(“n r Function to execute (1 for string
compare, 2 of character search, 3 for sub-string
2
search, or -1 to exit): “);
readInt(&choice);
if(choice == -1) break;
switch(choice){
case 1:
/* Read in the strings and store them in str1
and str2. */
status = compareStrings(str1, str2);
writeInt(status);
break;
case 2:
/* Read in the string, store it in str1; the
character in ch. */
status = searchForChar(str1, ch);
writeInt(status);
break;
case 3:
/* Read in the string, store it in str1; the
substring in str2. */
status = searchSubString(str1, str2);
writeInt(status);
break;
default:
writeString(“n r Erroneous entry. Try again.”);
};
}
writeString(“n r All done. Idling the CPU.”);
for(;;)
{
/* Let the microprocessor run. */
}
}
3
The following code snippet can be used to read string input from the terminal.
/* Read a string from the terminal via the UART. */
void readString(char *buffer)
{
uint32 rxData;
char *ptr = buffer;
for(;;){
rxData = UART_1_UartGetChar();
if(rxData){
UART_1_UartPutChar(rxData); /* Echo character. */
/* On a carriage return, null terminate the string
and break out of the loop. */
if(rxData == ’r’){
*ptr = ’ ’;
break;
}
*ptr = rxData;
ptr++;
}
}
}
Date:
Signature of the teaching assistant:
4
Lab 3b: Word Count
(15 points) Write a program that reads a line of text from the terminal, counts the number of words,
and identifies the length of the longest word and the greatest number of vowels in a word.
Date:
Signature of the teaching assistant:
5
Lab 3c: Luhn Checksum Validation
(20 points) The Luhn formula is a simple checksum formula used to validate credit card numbers.
It was designed to protect against accidental errors: most credit cards and many other government
identification numbers use the algorithm as a simple method of distinguishing valid numbers from
mistyped or otherwise incorrect numbers. The formula works as follows. Using the original number, double the value of every other digit. Then add the values of the individual digits together (if
a doubled value now has two digits, add the digits individually). The identification number is valid
if the resulting sum is divisible by 10.
Check digit
1
7
+
1
2
+
4
8
1
4
4
1
1
6
+
4
+
6
4
+
4
+
+
1
3
6
=
6
+
27
3
= 30
(a) Computing the check digit.
1
7
1
1
+
1
+
6
2
4
8
1
4
4
4
+
6
+
4
+
4
+
1
+
3
6
6
+
3
= 30
(b) Luhn checksum validation.
Fig. 1: The process of creating and validating Luhn checksums.
Let’s now walk through both ends of the checksum creation and validation process: computing a
check digit and validating the result. Figure 1(a) illustrates the steps involved in computing the
check digit. The original number, 176248, is shown in the dashed-line box. Every other digit,
starting from the rightmost digit of the original number (which after the addition of the check
digit, will be the second rightmost) is doubled. Then each digit is added together. Note that when
doubling a digit results in a two-digit number, each of these digits is considered separately. For
example, when 7 is doubled to produce 14, it is not 14 that is added to the checksum, but 1 and 4
separately. In our example, the generated checksum is 27; so the check digit is 3 because that’s the
6
digit value that makes the overall sum equal to 30. Recall that the checksum of the final number
should be divisible by 10; in other words, the checksum should end in 0.
Figure 1(b) illustrates the process of validating the number 1762483, which now includes the check
digit. As before, we double every second digit starting with the digit immediately to the left of the
check digit, and add the values of all digits, including the check digit, to determine the checksum.
Since the checksum is divisible by 10, this number validates. As another example, 79927398713
is valid as well.
Write a function called validateLuhn that takes as input an identification number and returns 1
if the number is a valid Luhn checksum, 0 otherwise. The identification number should be provided
by the user via the terminal and the result displayed on the terminal as well.
To check that your code is working correctly, 79927398713 is valid. Moreover, note that any credit
card number should result in a valid Luhn checksum.
Signature of the teaching assistant:
Date:
7
Lab 3d: Checking for Palindromes
(10 points) A palindrome is a word, phrase, or sentence that reads the same backward as forward;
some examples include: madam, nurses run, and poor dan is in a droop. Write a function called
checkPalindrome that takes as input a string comprising all lower-case alphabets including
spaces from the terminal and returns 1 if the string is a palindrome, 0 otherwise. Display the result
on the terminal.
Date:
Signature of the teaching assistant:
8
…
Purchase answer to see full
attachment
You will get a plagiarism-free paper and you can get an originality report upon request.
All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more