25.7k views
4 votes
Write a standalone C program that uses strings to have the user enter their contact information from the keyboard.

Validate the data entered by the user as follows:
o First Name: all alpha characters and spaces
o Last Name: all alpha characters and spaces
o Street: all alpha-numeric characters, spaces, comma, dash, hashtag and period
o State Abbreviation: two capital alpha characters
o Zipcode: all numeric characters and dash
o Phone number: be of the format
o City: all alpha characters and spaces
• If the user enters invalid data, use loops to re-prompt them to enter valid data reminding them of what they should be entering Your named constant, programmer-defined identifiers, function prototypes and headers, etc. should be meaningful and follow good programming practices.

1 Answer

5 votes

Final answer:

To create a standalone C program that validates contact information, use loops to repeatedly prompt the user and validate each input using string functions in C. Display appropriate error messages if the data does not meet the validation rules. Once all data is entered and validated, display a success message.

Step-by-step explanation:

To create a standalone C program that validates the user's contact information, you can use the following steps:

  1. Declare variables to store the user's input for first name, last name, street, state abbreviation, zipcode, and phone number.
  2. Use loops to repeatedly prompt the user to enter their contact information until valid data is entered.
  3. For each input, use the appropriate string functions in C to check if the entered data meets the specified validation rules.
  4. If the data does not meet the validation rules, display an appropriate error message and ask the user to re-enter the data.
  5. Once all the data has been entered and validated, display a message confirming that the contact information has been successfully entered.

Here's an example of a C program that implements the above steps:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char firstName[100];
char lastName[100];
char street[100];
char state[3];
char zipcode[10];
char phone[15];
char city[100];

while (1) {
// Prompt and read first name
printf('Enter First Name: ');
fgets(firstName, sizeof(firstName), stdin);
firstName[strcspn(firstName, '
')] = 0; // Remove trailing newline

// Validate first name
int i;
for (i = 0; i < strlen(firstName); i++) {
if (!isalpha(firstName[i]) && firstName[i] != ' ') {
printf('Invalid First Name. Please enter only alphabetic characters and spaces.\\');
break;
}
}
if (i == strlen(firstName))
break;
}

// Repeat the code above for last name, street, state, zipcode, phone, and city

printf('Contact Information entered successfully!\\');
return 0;
}
User Martin Svalin
by
8.4k points