-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path45_my_printf.c
161 lines (146 loc) · 2.98 KB
/
45_my_printf.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/****************************************************
DOCUMENTATION:
NAME :V.KARTHIEKEYAN
DATE :27.07.2021
DESCRIPTION :To implement printf function
OUTPUT : ./a.out
ENTER NUMBER, CHAR, STRING :2,c,hello
2 c hello
ENTER FLOAT : 2.65
2.650000
ENTER DOUBLE : 3.254
3.254000
DO YOU WANT TO CONTINUE(Y/y): n
******************************************************/
#include<stdio.h>
#include<stdarg.h>
void my_printf(const char *, ...);
int my_itoa(int , char *str);
void my_floatToString(float, char*, int);
int main()
{
char choice;
do
{
int num;
char c;
char *str;
float f;
double d;
my_printf("\nENTER NUMBER, CHAR, STRING :");
scanf("%d%*c%c%*c%s", &num, &c, str);
my_printf("%d %c %s",num, c, str);
my_printf("\nENTER FLOAT : ");
scanf("%f",&f);
my_printf("%f ",f);
my_printf("\nENTER DOUBLE : ");
scanf("%lf",&d);
my_printf("%f ", d);
my_printf("\nDO YOU WANT TO CONTINUE(Y/y): ");
scanf(" %c",&choice);
} while(choice == 'y' || choice =='Y');
return 0;
}
void my_printf(const char *arg, ...)
{
va_list ap;
int num;
char c;
char *str, number[50], float_string[50],double_string[50];
float f;
double d;
va_start(ap, arg);
while(*arg)
{
if(*arg == '%' )
{
arg++;
switch(*arg)
{
case 'd':
num = va_arg(ap, int);
my_itoa(num,number);
for(int i=0; number[i] != '\0'; i++)
putchar(number[i]);
arg++;
break;
case 'c':
c = va_arg(ap, int);
putchar(c);
arg++;
break;
case 's':
str = va_arg(ap, char *);
while(*str)
putchar(*str++);
arg++;
break;
case 'f':
f = va_arg(ap, double);
my_floatToString(f, float_string, 6);
for(int i=0; float_string[i] != '\0'; i++)
putchar(float_string[i]);
arg++;
break;
case 'l':
arg++;
if(*arg == 'f')
{
d = va_arg(ap, double);
my_floatToString(f, double_string, 6);
for(int i=0; double_string[i] != '\0'; i++)
putchar(double_string[i]);
arg++;
}
else
arg--;
break;
}
}
else
{
putchar(*arg);
arg++;
}
}
}
/* to convert float to float to string*/
void my_floatToString(float f, char *str, int precision)
{
int i = 0, j = 0, power=1;
int d = f;
i = my_itoa(d, str);
str[i++] = '.';
f = f - d; //float - integer not gives round off value
for(j; j < precision ; j++)
power *= 10;
f = f + (0.1/(float)power); //as float contains .____9999
f = (f*power);
j = my_itoa((int)f, &str[i]);
i = i+j;
for(j; j < 6; j++)
str[i++] = '0';
str[i]='\0';
}
/*to convert integer to string*/
int my_itoa(int num, char *str)
{
int i=0,j,count=0,temp=num;
if(num<0)
{
str[i++]='-'; //if negative str[0]='-' && increment the i
num *= -1;
}
while(temp>0) //how many digits are in the number
{
i++;
temp=temp/10;
}
str[i]='\0'; //null at the end of 'i' characters
for(j=i-1;num!=0;j--) //for string i-1
{
str[j]=num%10+48; //get the digit convert to char
num=num/10; //reduce the last digit
}
return i;
}