-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs392_printf.c
123 lines (98 loc) · 2.05 KB
/
cs392_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
//I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
int getSize (char * s) {
char * t;
for (t = s; *t != '\0'; t++)
;
return t - s;
}
char* reverse(int temp, char* temp2){
//OK IDEA IS MOD EACH VAL
//char* flipped;
int temp3=0;
int count=0;
//int length=getSize(val);
do{
temp3=temp%10;
temp2[count]=temp3+48;
//counter++;
temp=temp/10;
count++;
}while(temp!=0);
return temp2;
}
int cs392_printf(const char * format, ...){
va_list args;
va_start(args, format);
int counter=0;
//iterative over “format” and find specifiers
while (*format != '\0') {
// get the first argument with va_arg
// the following is an example of handling integer format “%d”
if(*format == '%'){
format ++;
if(*format == 0)
break;
if(*format == 'd'){
int arg_int = va_arg(args, int);
// this helps you get the argument corresponding to “%d”
int temp=arg_int;
int get_nums=arg_int;
char* temp2;
char* copy;
int temp3=0;
int count=0;
//convert int to char*, and then mod 10 -> count bytes (amt chars)
do{
temp3=temp%10;
temp2[count]=temp3+48;
counter++;
temp=temp/10;
count++;
}while(temp!=0);
count=0;
temp=arg_int;
do{
temp3=temp%10;
temp2[count]=temp3+48;
//counter++;
temp=temp/10;
count++;
}while(temp!=0);
//null case (ends line)
temp2[count]='\0';
//actual printing
//in order that it is all on one line
int index=count;
while(index>-1){
putc(temp2[index], stdout);
index--;
}
//puts(temp2);
}
if (*format == 's') {
char *arg_str = va_arg(args, char*);
int size=strlen(arg_str);
fputs(arg_str, stdout);
//strlen
counter+=size;
}
if (*format == 'c') {
char arg_char = va_arg(args, int);
fputc(arg_char,stdout);
}
}
//prints regular string
else {
char arg_char = *format;
putc(arg_char, stdout);
counter++;
}
++format;
}
va_end(args);
putc('\n',stdout);
return counter;
}