In: Computer Science
A simple c program is needed to create encrypt and decode social security numbers. Then a validation must be used to make sure there are no invalid numbers.
**********************************************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 that will accept as input a properly formatted
social security number.
Then increment each digit by one. If the digit is "9" then make it
a "0". Return a properly formatted
social security number.
Then create a function that will do the opposite. This function
will take an encoded social security
number and convert it back into an unencrypted social security
number. The de-encoded number is
what this function will return.
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.
C code
#include<string.h>
#include<stdio.h>
//encode
char *encrypt(char *ch)
{
for(int i=0;i<11;i++)
{
if(ch[i]=='9')
ch[i]='0';
else if(ch[i]=='-')
{
}
else
ch[i]=(char)((int)(ch[i])+1);
//printf("%s\n",ch);
}
return ch;
}
//en-decode
char *decrypt(char *ch)
{
for(int i=0;i<11;i++)
{
if(ch[i]=='0')
ch[i]='9';
else if(ch[i]=='-')
{
}
else
ch[i]=(char)((int)(ch[i])-1);
//printf("%s\n",ch);
}
return ch;
}
//check is valid or not
void *check(char *str , int pat)
{
char *ptr = strtok(str, "-");
int i = atoi(ptr);
printf( "%s \n" , str);
if(i<=740)
{
printf( "Invalid Patient %d : %s area number \n" , pat
, str);
return ;
}
int j=1;
while(ptr != NULL)
{
int k = atoi(ptr);
if(k==0)
{
if(j==1)
{
printf( "Invalid Patient %d : %s area number \n" , pat
, str);
}
if(j==2)
{
printf( "Invalid Patient %d : %s group number \n" ,
pat ,str );
}
if(j==3)
{
printf( "Invalid Patient %d : %s serial number \n" ,
pat , str );
}
return j;
}
ptr = strtok(NULL, "-");
j++;
}
printf( "Valid \n" );
}
int 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 size = sizeof(social)/sizeof(social[0]);
for(int i=0 ; i<size ; i++)
{
char *enc = encrypt(social[i]);
printf("Encrypted %s\n" ,enc) ;
char *dec = decrypt(enc);
printf("Decrypted %s\n" ,dec) ;
check(dec, i+1);
printf( "\n" );
}
return 0;
}