forked from sdsubhajitdas/Compiler-Design-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.c
67 lines (50 loc) · 1.32 KB
/
3.c
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
67
#include <stdio.h>
#include <string.h>
#include <math.h>
int getSize(char []);
void getType(char [], char [], int);
/*
Input: malloc(25*sizeof(int))
Output: calloc(25,sizeof(int))
Input: malloc(9*sizeof(float))
Output: calloc(9,sizeof(float))
Input: malloc(256*sizeof(double))
Output: calloc(256,sizeof(double))
*/
int main(){
char string[500],type[20];
int size,i;
printf("Enter the malloc statement\n");
scanf("%[^\n]%*c", string); // string input with spaces.
size = getSize(string);
i = 7 + floor(log10(size))+ 1 + 7 + 1; // Index of sizeof bracket
getType(string, type, i);
printf("calloc(%d,sizeof(%s))",size,type);
return 0;
}
void getType(char string[], char type[],int start){
/* Separates the type used in malloc
Eg: malloc(25*sizeof(int))
Separates int
*/
int idx;
for(idx=start; string[idx] != '\0'; idx++)
if(string[idx] == ')')
break;
else
type[idx-start] = string[idx];
type[idx] = '\0';
}
int getSize(char string[]){
/* Returns the size used in malloc
Eg: malloc(25*sizeof(int))
Returns 25
*/
int idx, size = 0;
for(idx=7; string[idx] != '\0'; idx++)
if(string[idx] == '*')
break;
else
size = size*10 + (string[idx] - '0');
return size;
}