|
| 1 | +//I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdarg.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | + |
| 8 | +int getSize (char * s) { |
| 9 | + char * t; |
| 10 | + for (t = s; *t != '\0'; t++) |
| 11 | + ; |
| 12 | + return t - s; |
| 13 | +} |
| 14 | + |
| 15 | +char* reverse(int temp, char* temp2){ |
| 16 | + //OK IDEA IS MOD EACH VAL |
| 17 | + //char* flipped; |
| 18 | + int temp3=0; |
| 19 | + int count=0; |
| 20 | + //int length=getSize(val); |
| 21 | + do{ |
| 22 | + temp3=temp%10; |
| 23 | + temp2[count]=temp3+48; |
| 24 | + //counter++; |
| 25 | + temp=temp/10; |
| 26 | + count++; |
| 27 | + }while(temp!=0); |
| 28 | + |
| 29 | + return temp2; |
| 30 | +} |
| 31 | + |
| 32 | +int cs392_printf(const char * format, ...){ |
| 33 | + |
| 34 | +va_list args; |
| 35 | +va_start(args, format); |
| 36 | +int counter=0; |
| 37 | + |
| 38 | +//iterative over “format” and find specifiers |
| 39 | +while (*format != '\0') { |
| 40 | + |
| 41 | + // get the first argument with va_arg |
| 42 | + // the following is an example of handling integer format “%d” |
| 43 | + if(*format == '%'){ |
| 44 | + |
| 45 | + format ++; |
| 46 | + if(*format == 0) |
| 47 | + break; |
| 48 | + |
| 49 | + if(*format == 'd'){ |
| 50 | + int arg_int = va_arg(args, int); |
| 51 | + // this helps you get the argument corresponding to “%d” |
| 52 | + |
| 53 | + int temp=arg_int; |
| 54 | + int get_nums=arg_int; |
| 55 | + char* temp2; |
| 56 | + char* copy; |
| 57 | + int temp3=0; |
| 58 | + int count=0; |
| 59 | + |
| 60 | + //convert int to char*, and then mod 10 -> count bytes (amt chars) |
| 61 | + do{ |
| 62 | + temp3=temp%10; |
| 63 | + temp2[count]=temp3+48; |
| 64 | + counter++; |
| 65 | + temp=temp/10; |
| 66 | + count++; |
| 67 | + }while(temp!=0); |
| 68 | + |
| 69 | + |
| 70 | + count=0; |
| 71 | + temp=arg_int; |
| 72 | + do{ |
| 73 | + temp3=temp%10; |
| 74 | + temp2[count]=temp3+48; |
| 75 | + //counter++; |
| 76 | + temp=temp/10; |
| 77 | + count++; |
| 78 | + }while(temp!=0); |
| 79 | + |
| 80 | + //null case (ends line) |
| 81 | + temp2[count]='\0'; |
| 82 | + //actual printing |
| 83 | + |
| 84 | + //in order that it is all on one line |
| 85 | + int index=count; |
| 86 | + while(index>-1){ |
| 87 | + putc(temp2[index], stdout); |
| 88 | + index--; |
| 89 | + } |
| 90 | + //puts(temp2); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + if (*format == 's') { |
| 95 | + char *arg_str = va_arg(args, char*); |
| 96 | + |
| 97 | + int size=strlen(arg_str); |
| 98 | + fputs(arg_str, stdout); |
| 99 | + //strlen |
| 100 | + counter+=size; |
| 101 | + } |
| 102 | + |
| 103 | + if (*format == 'c') { |
| 104 | + char arg_char = va_arg(args, int); |
| 105 | + fputc(arg_char,stdout); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + //prints regular string |
| 110 | + else { |
| 111 | + char arg_char = *format; |
| 112 | + putc(arg_char, stdout); |
| 113 | + counter++; |
| 114 | + } |
| 115 | + |
| 116 | + ++format; |
| 117 | + } |
| 118 | + |
| 119 | + va_end(args); |
| 120 | + putc('\n',stdout); |
| 121 | + return counter; |
| 122 | + |
| 123 | +} |
0 commit comments