Commit d45e6369 by chazog

Upload New File

parent 92924cb5
Showing with 174 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
/*
* These are the function prototypes
*/
int valDec(char *string);
int valHex(char *string);
int valOct(char *string);
int valBin(char *string);
int main()
{
int p, base;
char string[100];
printf("Please enter the string:\n");
scanf("%s", string);
printf("Please Specify the base you would like to validate in\n1-Decimal\t 2-Hexadecimal\t\t 3-Octal\t 4-Binary\n");
scanf("%d", &base);
switch (base)
{
case 1:
p = valDec(string);
if (p == 0)
{
printf("The given input string is in fact, a valid decimal integer");
}
else
{
printf("The given input string is NOT a valid decimal integer");
}
break;
case 2:
p = valHex(string);
if (p == 0)
{
printf("The given input string is in fact, a valid Hexadecimal");
}
else
{
printf("The given input string is NOT a valid Hexadecimal");
}
break;
case 3:
p = valOct(string);
if (p == 0)
{
printf("The given input string is in fact, a valid Octadecimal");
}
else
{
printf("The given input string is NOT a valid Octadecimal");
}
break;
case 4:
p = valBin(string);
if (p == 0)
{
printf("The given input string is in fact, a valid Binary number");
}
else
{
printf("The given input string is NOT a valid Binary number");
}
break;
default:
printf("Error. Invalid choice!");
break;
}
return 0;
}
int valDec(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (!isdigit(string[i]))
{
val = 1;
}
else
{
val = 0;
}
}
return val;
}
int valHex(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (!isxdigit(string[i]))
{
val = 1;
}
else
{
val = 0;
}
}
return val;
}
int valOct(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == '0' || string[i] == '1' || string[i] == '2' || string[i] == '3' || string[i] == '4' || string[i] == '5' || string[i] == '6' || string[i] == '7')
{
val = 0;
}
else
{
val = 1;
}
}
return val;
}
int valBin(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == '0' || string[i] == '1')
{
val = 0;
}
else
{
val = 1;
}
}
return val;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment