forked from annuraagggIIIT/Problem-Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataInputFromUser.c
More file actions
66 lines (52 loc) · 1.25 KB
/
dataInputFromUser.c
File metadata and controls
66 lines (52 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#define SIZE 255
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Datatype from user input can be integer, float, character or string.
bool isInteger(char *input)
{
char *endptr;
strtol(input, &endptr, 10);
return (*endptr == '\0');
}
bool isFloat(char *input)
{
for (int i = 0; i < strlen(input); i++)
{
if (input[i] == ',')
input[i] = '.'; // Replace comma with dot
}
char *endptr;
strtof(input, &endptr);
return (*endptr == '\0');
}
bool isCharacter(char *input)
{
return (strlen(input) == 1);
}
char *dataInputFromUser(char *input)
{
char *result;
result = (char *)malloc(SIZE * sizeof(char));
if (isInteger(input))
result = "Integer";
else if (isFloat(input))
result = "Float";
else if (isCharacter(input))
result = "Character";
else
result = "String";
return result;
}
int main()
{
char *input;
input = (char *)malloc(SIZE * sizeof(char));
printf("Enter something: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove the newline character from input
printf("You entered a %s datatype", dataInputFromUser(input));
free(input);
return 0;
}