Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
chazog
/
iax0583
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d45e6369
authored
Dec 16, 2018
by
chazog
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
92924cb5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
174 additions
and
0 deletions
Lab9/Task4.c
Lab9/Task4.c
0 → 100644
View file @
d45e6369
#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
\n
1-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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment