In: Computer Science
*****************************C PROGRAMMING****************************************************
char social[][12] = {"164-55-0726","948-44-1038","193-74-0274","458-57-2867","093-00-1093","159-56-9731","695-21-2340","753-66-6482", "852-73-4196","648-81-1456","879-61-1829","123-87-0000","000-65-3197","741-85-9632","963-25-7418"};
Create a function to scan all of the patient's social security
numbers and detect if any of them are
invalid.
A Social Security Number (SSN) consists of nine digits, commonly
written as three fields separated by
hyphens: AAA-GG-SSSS. The first three-digit field is called the
"area number". The central, two-digit field
is called the "group number". The final, four-digit field is called
the "serial number". Any SSN conforming
to one of the following criteria is an invalid number:
Any field all zeroes (no field of zeroes is ever
assigned).
First three digits above 740
If you detect an invalid social security number, print the
patient's name, their social security number,
and then either "area number", "group number", or "serial number"
to indicate where the problem with
the social security number was detected.
Below is the C code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
Note that since you have not provided patient's name corresponding to SSN, hence I will not be printing it
#include<stdio.h>
#include<conio.h>
void checkSSN(char social[15][12], int n)
{
int areaNumber, groupNumber, serialNumber;
int i;
char delim[] = "-"; //set a delimeter
char *ptr, *temp;
int lengthOfAreaNumber, lengthOfGroupNumber,
lengthOfSerialNumber;
for(i=0;i<n;i++) //loop through every SSN
{
temp = (char*)malloc(sizeof(char)*12);
strcpy(temp, social[i]);
ptr = strtok(temp, delim); //tokenize the SSN
for(lengthOfAreaNumber=0;ptr[lengthOfAreaNumber]!='\0';lengthOfAreaNumber++);
if(lengthOfAreaNumber!=3) //invalid length of area number
{
printf("Invalid area number %s\n", ptr);
continue;
}
areaNumber = atoi(ptr); //convert string to integer
if(areaNumber>740 || areaNumber==0) //invalid area number
{
printf("Invalid area number %d\n", areaNumber);
continue;
}
ptr = strtok(NULL, delim); //ptr now points to group number
for(lengthOfGroupNumber=0;ptr[lengthOfGroupNumber]!='\0';lengthOfGroupNumber++);
if(lengthOfGroupNumber!=2) //invalid length of group number
{
printf("Invalid group number %s %d\n", ptr,
lengthOfGroupNumber);
continue;
}
groupNumber == atoi(ptr); //convert string to integer
if(groupNumber == 0)
{
printf("Invalid serial number %s\n", ptr);
continue;
}
ptr = strtok(NULL, delim); //ptr now points to serial number
for(lengthOfSerialNumber=0;ptr[lengthOfSerialNumber]!='\0';lengthOfSerialNumber++);
if(lengthOfSerialNumber!=4) //invalid length of serial number
{
printf("Invalid serial number %s\n", ptr);
continue;
}
serialNumber = atoi(ptr);
if(serialNumber == 0)
{
printf("Invalid serial number %s\n", ptr);
continue;
}
printf("Valid SSN %s\n", social[i]);
}
}
void main()
{
char social[][12] =
{"164-55-0726","948-44-1038","193-74-0274","458-57-2867",
"093-00-1093","159-56-9731","695-21-2340","753-66-6482",
"852-73-4196",
"648-81-1456","879-61-1829","123-87-0000","000-65-3197","741-85-9632","963-25-7418"};
int n = 15; //number of elements
checkSSN(social, n);
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.