Question
#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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started