Write a Complete program for the following specification: The SerialNumber takes
ID: 3546178 • Letter: W
Question
Write a Complete program for the following specification:
The SerialNumber takes a software serial number in
the form of LLLLL-DDDD-LLLL where each L is a letter
and each D is a digit. The serial number has three groups
of characters, separated by hyphens. The class extracts
the three groups of characters and validates them.
Example:
String serial1= "GHTRJ-8975-AQWR"; // Valid
String serial2 = "GHT7J-8975-AQWR"; // Invalid
String serial3 = "GHTRJ-8J75-AQWR"; // Invalid
String serial4 = "GHTRJ-8975-AQ2R"; // Invalid
only using c language.... not c++ plz
Explanation / Answer
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int is_valid_letter(char *str)
{
int i=0;
for(i=0; str[i]!=0; i++)
{
if(!((str[i]>=65 && str[i] <=90) || (str[i]>=97 && str[i] <=122)))
return 0;
}
return 1;
}
int is_valid_int(char *str)
{
int i=0;
for(i=0; str[i]!=0; i++)
{
if(!(str[i]>=48 && str[i] <=57))
return 0;
}
return 1;
}
int is_valid_serial(char str1[])
{
char *str[3];
char *serial1 = (char*) malloc (strlen(str1)+1);
strcpy(serial1,str1);
int index = 0;
str[index] = strtok(serial1,"-");
while (str[index] != NULL)
{
//printf ("%s ",str);
str[++index] = strtok (NULL, "-");
}
if(!is_valid_letter(str[0]))
{
return 0;
}
else if(!is_valid_int(str[1]))
{
return 0;
}
else if(!is_valid_letter(str[2]))
{
return 0;
}
return 1;
}
int main()
{
char serial2[]= "GHTRJ-8975-AQWR"; // Valid
char serial1[] = "GHT7J-8975-AQWR"; // Invalid
char serial3[] = "GHTRJ-8J75-AQWR"; // Invalid
char serial4[] = "GHTRJ-8975-AQ2R"; // Invalid
printf("%s %s",serial1,(is_valid_serial(serial1)?"is valid":"is invalid"));
printf(" %s %s",serial2,(is_valid_serial(serial2)?"is valid":"is invalid"));
printf(" %s %s",serial3,(is_valid_serial(serial3)?"is valid":"is invalid"));
printf(" %s %s",serial4,(is_valid_serial(serial4)?"is valid":"is invalid"));
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.