Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include //STEP 1 - Complete the prototype for the function check_zip. // It requires 1 string and one int as parameters. void check_zip(

#include #include #include //STEP 1 - Complete the prototype for the function check_zip. // It requires 1 string and one int as parameters. void check_zip( ); int main() { char zip[6] = {'\0'}; check_zip(zip, 6); printf("You typed: %s ", zip); } //STEP 2 - Complete the header for the function check_zip. // It requires 1 string and one int as parameters. // The string holds a zip code, and the int refers // to the size of the array that holds this data. void check_zip(char str[], int l ){ // valid_data and valid_length are flag variables: // If a flag variable is 0, it means false. If it is 1, it means true. // We're using flags to report the outcome of checking the length of // the string, and whether or not it contains only digits. // Since we haven't performed these checks yet, we cannot say // that we have valid data. So we set both flags to false. int valid_data = 0; int valid_length = 0; // length_of_string holds the length of the string int length_of_string; // This outer loop continues to execute while the string contains invalid // data (either it is an incorrect length or it contains a // character that is not a digit). while (!valid_data) { //Prompt for data entry puts("Type in a zip code"); //STEP 3 - Complete the statement using the parameter // in the function header that refers to the zip code string gets(str); //STEP 4 - Complete the step using a string function to get the // CURRENT LENGTH of the string. length_of_string = strlen(str); // If the length is the correct length for a zip code, the valid // length flag is updated to true.

if (length_of_string == 5){ valid_length = 1; } // If the length is not the correct length for a zip code, an error // message is printed, the valid length flag is not updated, and // we can skip the next check and continue with // the next iteration of the loop. else { puts("Not correct length"); continue; } // valid_digit is another flag variable.This time, we assume all digits // are valid until proven otherwise. So this flag is set to true. int valid_digit = 1; // Used as an index to each character in the string int i = 0; //STEP 5 - Complete the inner loop using a while statement. // The loop should continue while: // We haven't found an invalid digit, and // We haven't reached the end of the string. while (valid_digit && i < l - 1) { //STEP 6 - Complete the if condition. // The condition should be TRUE if the current character is NOT // a digit. You must use one of the character classification // functions. if (!isdigit(str[i])) { //STEP 7 - Complete the print statement using the parameter defined above // that refers to the zip code string printf("%c is not a digit ", str[i]); // We have found an invalid digit, so this flag is set to false. // The inner loop will stop. valid_digit = 0; } else { // Update the index i++; } } // At the end of the outer loop, check both flags. // If the length and the digits are all valid, set the LCV valid_data to true. // This will stop the outer loop. if (valid_length && valid_digit)

valid_data = 1; } // If we reach this statement, the data has passed the validity checks. puts("Valid zip code"); }

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_2

Step: 3

blur-text-image_3

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

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions