Skip to content

Commit 61a0d18

Browse files
authored
Add files via upload
0 parents  commit 61a0d18

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

cs392_printf.c

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

cs392_printf.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "cs392_printf.c"
2+
3+
//prot
4+
static int cs392_fprintf(const char *format, ...);

practice.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
3+
4+
[1] Create an integer variable
5+
[2] Assign a constant value to that variable (whatever value you want)
6+
[3] Create an integer pointer
7+
[4] Make that pointer point to the integer variable
8+
[5] Change that integer variable to another constant value by dereferencing the pointer created in [3] (instead of directly changing that integer variable)
9+
[6] Print the integer variable again.
10+
[7] Please upload your .c file.
11+
*/
12+
13+
#include <stdio.h>
14+
15+
int main()
16+
{
17+
int x = 10; // steps 1 & 2
18+
int *pointy = &x; // steps 3 & 4
19+
20+
printf("x = %d\n",x); // print the og variable to compare change
21+
(*pointy)+=2; //step 5
22+
printf("x = %d\n",x); // step 6
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)