├── .gitignore ├── Makefile ├── bit_print.c ├── calc.c ├── calc_adv.c ├── cbd_lawn.c ├── cbd_pennies.c ├── cbd_pennies_adv.c ├── cbd_rand.c ├── cbd_rand_dist.c ├── cbd_time.c ├── cbd_triple_int.c ├── char_arrays.c ├── chtp_2-12.c ├── chtp_2-16.c ├── chtp_2-17.c ├── chtp_2-20.c ├── chtp_2-9.c ├── chtp_2_26.c ├── chtp_2_27.c ├── chtp_2_28.c ├── chtp_3_16.c ├── chtp_3_17.c ├── chtp_3_18.c ├── chtp_3_19.c ├── chtp_3_22.c ├── chtp_3_23.c ├── chtp_3_24.c ├── convert.c ├── countchar.c ├── digit_count.c ├── double.c ├── ex_1-10.c ├── ex_1-12.c ├── ex_1-13.c ├── ex_1-14.c ├── ex_1-14a.c ├── ex_1-15.c ├── ex_1-16.c ├── ex_1-17.c ├── ex_1-19.c ├── ex_1-23.c ├── ex_1-3.c ├── ex_1-4.c ├── ex_1-5.c ├── ex_1-7.c ├── ex_1-8.c ├── ex_1-9.c ├── ex_2-4.c ├── ex_2-5.c ├── ex_3-3.c ├── ex_3-5.c ├── ex_3-6.c ├── ex_4-1.c ├── ex_4-12.c ├── ex_4-13.c ├── ex_4-2.c ├── ex_5-1.c ├── ex_5-10.c ├── ex_5-13.c ├── ex_5-14.c ├── ex_5-3.c ├── ex_5-4.c ├── ex_5-5.c ├── ex_5-7.c ├── ex_5-8.c ├── for.c ├── get_chars.c ├── hello.c ├── limits_of_c_lang.c ├── line_counter.c ├── primes.c ├── ranges.c ├── secure.c ├── shell_sort.c ├── srs_lib.c ├── srs_lib.h ├── stack_address.c ├── struct.c ├── temp_conv.c ├── temp_conv2.c ├── test_get_char.c ├── variable_arrays.c └── word_count.c /.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !/**/ 3 | !*.* 4 | *~ 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -Werror -g 3 | LIBS = -lm 4 | SOURCES = $(wildcard *.c) 5 | EXECUTABLES = $(SOURCES:.c=) 6 | 7 | all: $(EXECUTABLES) 8 | 9 | %: %.c 10 | $(CC) $(CFLAGS) $< -o $@ $(LIBS) 11 | 12 | clean: 13 | rm -f $(EXECUTABLES) 14 | -------------------------------------------------------------------------------- /bit_print.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Prints out the bits contained in an integer value. 4 | 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | void bit_print(int a) 11 | { 12 | int i; 13 | int n = sizeof(int) * CHAR_BIT; /* find size of ints for this macine */ 14 | int mask = 1 << (n - 1); /* set mask to 1000000...000 */ 15 | 16 | for(i = 1; i <= n; ++i) 17 | { 18 | putchar(((a & mask) == 0) ? '0' : '1'); 19 | a <<= 1; /* shift over by one bit */ 20 | if(i % CHAR_BIT == 0 && i < n) 21 | putchar(' '); 22 | } 23 | putchar('\n'); 24 | 25 | } 26 | 27 | int main() 28 | { 29 | int number; 30 | 31 | for(number = 0; number <= 16; ++number) 32 | { 33 | bit_print(number); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /calc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAXOP 100 6 | #define NUMBER '0' 7 | #define MAXVAL 100 8 | #define BUFSIZE 100 9 | 10 | int getop(char[]); 11 | void push(double); 12 | double pop(void); 13 | 14 | int main() 15 | { 16 | int type; 17 | double op2; 18 | char s[MAXOP]; 19 | 20 | while((type = getop(s)) != EOF) 21 | { 22 | switch(type) 23 | { 24 | case NUMBER: 25 | push(atof(s)); 26 | break; 27 | case '+': 28 | push(pop() + pop()); 29 | break; 30 | case '*': 31 | push(pop() * pop()); 32 | break; 33 | case '-': 34 | op2 = pop(); 35 | push(pop() - op2); 36 | break; 37 | case '/': 38 | op2 = pop(); 39 | if(op2 != 0.0) 40 | push(pop() / op2); 41 | else 42 | printf("error: zero divisor\n"); 43 | break; 44 | case '\n': 45 | printf("\t%.8g\n", pop()); 46 | break; 47 | default: 48 | printf("error: unknown command %s\n", s); 49 | break; 50 | } 51 | } 52 | return 0; 53 | } 54 | 55 | 56 | 57 | 58 | int sp = 0; 59 | double val[MAXVAL]; 60 | 61 | 62 | /* push f onto value stack */ 63 | void push(double f) 64 | { 65 | if(sp < MAXVAL) 66 | val[sp++] = f; 67 | else 68 | printf("error: stack full\n"); 69 | } 70 | 71 | 72 | /* pop and return top value from stack */ 73 | double pop(void) 74 | { 75 | if(sp > 0) 76 | return val[--sp]; 77 | else 78 | { 79 | printf("error: stack empty\n"); 80 | return 0.0; 81 | } 82 | } 83 | 84 | 85 | 86 | int getch(void); 87 | void ungetch(int); 88 | 89 | /* get next operator or numberic operand */ 90 | int getop(char s[]) 91 | { 92 | int i, c; 93 | 94 | while ((s[0] = c = getch()) == ' ' || c == '\t') 95 | ; 96 | s[1] = '\0'; 97 | if(!isdigit(c) && c != '.') 98 | return c; 99 | i = 0; 100 | if(isdigit(c)) 101 | while(isdigit(s[++i] = c = getch())) 102 | ; 103 | if(c == '.') 104 | while(isdigit(s[++i] = c = getch())) 105 | ; 106 | s[i] = '\0'; 107 | if(c != EOF) 108 | ungetch(c); 109 | return NUMBER; 110 | } 111 | 112 | char buf[BUFSIZE]; 113 | int bufp = 0; 114 | 115 | int getch(void) 116 | { 117 | return (bufp > 0) ? buf[--bufp] : getchar(); 118 | } 119 | 120 | 121 | void ungetch(int c) 122 | { 123 | if(bufp >= BUFSIZE) 124 | printf("ungetch: toomany characters\n"); 125 | else 126 | buf[bufp++] = c; 127 | } 128 | -------------------------------------------------------------------------------- /calc_adv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAXOP 100 7 | #define NUMBER '0' 8 | #define MAXVAL 100 9 | #define BUFSIZE 100 10 | 11 | void dup_stack(void); 12 | int getop(char[]); 13 | void push(double); 14 | double pop(void); 15 | void print_stack(void); 16 | void reverse(void); 17 | int getch(void); 18 | void ungetch(int); 19 | 20 | char buf[BUFSIZE]; 21 | int bufp = 0; 22 | int sp = 0; 23 | double val[MAXVAL]; 24 | static int ungetchar = '\0'; 25 | 26 | int main() 27 | { 28 | int type; 29 | double op2; 30 | char s[MAXOP]; 31 | 32 | while((type = getop(s)) != EOF) 33 | { 34 | switch(type) 35 | { 36 | case NUMBER: 37 | push(atof(s)); 38 | break; 39 | case '#': 40 | dup_stack(); 41 | break; 42 | case '?': 43 | print_stack(); 44 | break; 45 | case '~': 46 | reverse(); 47 | break; 48 | case '+': 49 | push(pop() + pop()); 50 | break; 51 | case '*': 52 | push(pop() * pop()); 53 | break; 54 | case '-': 55 | op2 = pop(); 56 | push(pop() - op2); 57 | break; 58 | case '/': 59 | op2 = pop(); 60 | if(op2 != 0.0) 61 | push(pop() / op2); 62 | else 63 | printf("error: zero divisor\n"); 64 | break; 65 | case '%': 66 | op2 = pop(); 67 | if(op2 != 0.0) 68 | { 69 | push((int)(pop()) % (int)(op2)); 70 | } else { 71 | printf("error: zero divisor\n"); 72 | } 73 | break; 74 | case 'c': 75 | push(cos(pop())); 76 | break; 77 | case 'p': 78 | push((int)pow(pop(),pop())); 79 | break; 80 | case 't': 81 | push(tan(pop())); 82 | break; 83 | case 'q': // square root 84 | push(sqrt(pop())); 85 | break; 86 | case 'r': 87 | push(rand()); 88 | break; 89 | case 's': 90 | push(sin(pop())); 91 | break; 92 | case '\n': 93 | printf("\t%.8g\n", pop()); 94 | break; 95 | default: 96 | printf("error: unknown command %s\n", s); 97 | break; 98 | } 99 | } 100 | return 0; 101 | } 102 | 103 | /* push f onto value stack */ 104 | void push(double f) 105 | { 106 | if(sp < MAXVAL) 107 | val[sp++] = f; 108 | else 109 | printf("error: stack full\n"); 110 | } 111 | 112 | /* pop and return top value from stack */ 113 | double pop(void) 114 | { 115 | if(sp > 0) 116 | return val[--sp]; 117 | else 118 | { 119 | printf("stack empty\n"); 120 | return 0.0; 121 | } 122 | } 123 | 124 | /* duplicate value at top of stack */ 125 | void dup_stack() 126 | { 127 | int temp = pop(); 128 | push(temp); 129 | push(temp); 130 | } 131 | 132 | /* reverse two vales at top of stack */ 133 | void reverse() 134 | { 135 | double temp1, temp2; 136 | temp1 = pop(); 137 | temp2 = pop(); 138 | push(temp1); 139 | push(temp2); 140 | } 141 | 142 | /* print top two values on stack */ 143 | void print_stack() 144 | { 145 | if(sp > 0) 146 | printf("%f\t%f\n", val[sp], val[sp-1]); 147 | // else 148 | // printf("Stack is empty!\n"); 149 | } 150 | 151 | /* get next operator or numberic operand */ 152 | int getop(char s[]) 153 | { 154 | int i, c; 155 | 156 | while ((s[0] = c = getch()) == ' ' || c == '\t') 157 | ; 158 | s[1] = '\0'; 159 | if(!isdigit(c) && c != '.') 160 | return c; 161 | i = 0; 162 | if(isdigit(c)) 163 | while(isdigit(s[++i] = c = getch())) 164 | ; 165 | if(c == '.') 166 | while(isdigit(s[++i] = c = getch())) 167 | ; 168 | s[i] = '\0'; 169 | if(c != EOF) 170 | c = ungetchar; 171 | return NUMBER; 172 | } 173 | 174 | int getch(void) 175 | { 176 | return (bufp > 0) ? buf[--bufp] : getchar(); 177 | } 178 | 179 | /* NO LONGER NEEDED 180 | void ungetch(int c) 181 | { 182 | if(bufp >= BUFSIZE) 183 | printf("ungetch: too many characters\n"); 184 | else 185 | buf[bufp++] = c; 186 | } 187 | */ 188 | -------------------------------------------------------------------------------- /cbd_lawn.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int width, height; 6 | int cv_factor = 36 * 36; 7 | 8 | printf("Calculate area of rectangular lawn.\nUnits should be input in yards.\n"); 9 | printf("Width: "); 10 | scanf("%d", &width); 11 | printf("Height: "); 12 | scanf("%d", &height); 13 | 14 | printf("Area Square Yards: %d\n", width * height); 15 | printf("Area Square Feet: %d\n", (width * height) * cv_factor); 16 | printf("Area Square Inches: %d\n", width * height * cv_factor * 12); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /cbd_pennies.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int h, // half dollars 6 | q, // quarters 7 | n, // nickels 8 | p, // pennies 9 | coins; 10 | 11 | printf("Calculates value of change.\n"); 12 | printf("Half Dollars: "); 13 | scanf("%d", &h); 14 | printf("Quarters: "); 15 | scanf("%d", &q); 16 | printf("Nickels: "); 17 | scanf("%d", &n); 18 | printf("Pennies: "); 19 | scanf("%d", &p); 20 | 21 | printf("Half Dollars: %d\n", h); 22 | printf("Quarters: %d\n", q); 23 | printf("Nickels: %d\n", n); 24 | printf("Pennies: %d\n", p); 25 | 26 | coins = h+q+n+p; 27 | p = p + (h*50) + (q*25) + (n*5); 28 | 29 | printf("The value of your %d coins is %d pennies.\n", coins, p); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /cbd_pennies_adv.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int h, // half dollars 6 | q, // quarters 7 | n, // nickels 8 | p, // pennies 9 | coins; 10 | float total; 11 | 12 | printf("Calculates value of change.\n"); 13 | printf("Half Dollars: "); 14 | scanf("%d", &h); 15 | printf("Quarters: "); 16 | scanf("%d", &q); 17 | printf("Nickels: "); 18 | scanf("%d", &n); 19 | printf("Pennies: "); 20 | scanf("%d", &p); 21 | 22 | printf("Half Dollars: %d\n", h); 23 | printf("Quarters: %d\n", q); 24 | printf("Nickels: %d\n", n); 25 | printf("Pennies: %d\n", p); 26 | 27 | coins = h+q+n+p; 28 | total = (p * .01) + (h * .50) + (q * .25) + (n * .5); 29 | 30 | printf("The value of your %d coins is $%.2f.\n", coins, total); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /cbd_rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int i, n, col; 7 | 8 | printf("\n%s\n%s", 9 | "Some randomly distributed integers. ", 10 | "How many to print? "); 11 | scanf("%d", &n); 12 | printf("How many columns? "); 13 | scanf("%d", &col); 14 | 15 | for(i=0; i < n; ++i) 16 | { 17 | if(i % col == 0) { printf("\n"); } 18 | printf("%d", rand()%10); 19 | } 20 | printf("\n"); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /cbd_rand_dist.c: -------------------------------------------------------------------------------- 1 | /* 2 | * User inputs integer number. This number represents the count of random numbers to generate. 3 | * Each of these numbers is counted in an array of 10 integers. 4 | * When the number generation is complete, a chart of the distribution is sent to stdout. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | int count; 13 | int tally[10] = { '0' }; 14 | 15 | printf("Number of Random Integers to Generate: "); 16 | scanf("%d", &count); 17 | while(count > 0) 18 | { 19 | tally[rand()%10]++; 20 | --count; 21 | } 22 | 23 | while(count < 10) 24 | { 25 | printf("%d->%d\n", count, tally[count]); 26 | ++count; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /cbd_time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int input_value, minutes, seconds; 6 | 7 | printf("Input Number of seconds: "); 8 | scanf("%d", &input_value); 9 | minutes = input_value / 60; 10 | seconds = input_value % 60; 11 | printf("%d seconds is equivalent to %d minutes and %d seconds.\n", input_value, minutes, seconds); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /cbd_triple_int.c: -------------------------------------------------------------------------------- 1 | /* Find triples of integers that add up to N */ 2 | 3 | #include 4 | 5 | #define N 7 6 | 7 | int main() 8 | { 9 | int cnt = 0, i, j, k; 10 | 11 | for(i = 0; i <= N; ++i) 12 | for(j = 0; j <= N; ++j) 13 | for(k = 0; k <= N; ++k) 14 | if(i + j + k == N) 15 | { 16 | ++cnt; 17 | printf("%3d%3d%3d\n", i, j, k); 18 | } 19 | printf("\nCount: %d\n", cnt); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /char_arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAXLINE 1000 4 | 5 | int getline2(char line[], int maxline); 6 | void copy(char to[], char from[]); 7 | 8 | int main() 9 | { 10 | int len; /* current line length */ 11 | int max; /* maximum length seen so far */ 12 | char line[MAXLINE]; /* current input line */ 13 | char longest[MAXLINE]; /* longest line saved here */ 14 | 15 | max = 0; 16 | while ((len = getline2(line, MAXLINE)) > 0) 17 | if(len > max) 18 | { 19 | max = len; 20 | copy(longest, line); 21 | } 22 | if (max > 0) 23 | printf("%s", longest); 24 | return 0; 25 | } 26 | 27 | /* getline: read a line into s, return length */ 28 | int getline2(char s[], int lim) 29 | { 30 | int c, i; 31 | 32 | for (i = 0; i 2 | 3 | int main(){ 4 | int a = 15, b = 4, c = 7; 5 | printf("%d\n", a % b); 6 | printf("%d\n", a % c + b); 7 | printf("b=\n"); 8 | printf("a = 15\n"); 9 | printf("%d = a + b\n", a + b); 10 | printf("Enter an integer:\n"); 11 | scanf("%d%d", &a, &b); 12 | printf("%d, %d\n", a , b); 13 | 14 | a = 2; b = 4; 15 | a = b * a; 16 | 17 | printf("a = %d, b = %d \n", a, b); 18 | printf("Enter an integer:\n"); 19 | scanf("%d", &a); 20 | printf("Enter an integer:\n"); 21 | scanf("%d", &b); 22 | 23 | printf("a = %d, b = %d \n", a, b); 24 | puts("Hello World"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chtp_2-16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | printf("sum :%d\n", a + b); 7 | printf("product :%d\n", a * b); 8 | printf("difference :%d\n", a - b); 9 | printf("quotient :%d\n", a / b); 10 | printf("remainder :%d\n", a % b); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /chtp_2-17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int u, a, t; 5 | printf("Initial Velocity:"); 6 | scanf("%d", &u); 7 | 8 | printf("Initial Acceleration:"); 9 | scanf("%d", &a); 10 | 11 | printf("Time:"); 12 | scanf("%d", &t); 13 | 14 | int v = u + (a*t); 15 | int d = u * t + .5 * a* (t*t); 16 | 17 | printf("Velocity:%d\n", v); 18 | printf("Distance:%d\n", d); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /chtp_2-20.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int h, m, s; 4 | printf("Seconds:"); 5 | scanf("%d", &s); 6 | //h = s % (60*60); 7 | h = s/3600; 8 | s %= 3600 * h; 9 | m = s / 60; 10 | s %= 60 * m; 11 | 12 | printf("H: %d, M: %d, S: %d\n", h,m,s); 13 | } 14 | -------------------------------------------------------------------------------- /chtp_2-9.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | printf("%s", "Have a nice day."); 5 | int a = 0, b = 1, c = 2; 6 | a = b+c; 7 | if(a > b){ c = a - b; } 8 | printf("%d", a); 9 | int p, q, r, x; 10 | x = scanf("%d %d %d", &p, &q, &r); 11 | printf("%d, %d, %d, %d", p,q,r,x); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chtp_2_26.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x; 6 | printf("Enter an integer: "); 7 | scanf("%d", &x); 8 | printf("%d = %c\n", x, x); 9 | } 10 | -------------------------------------------------------------------------------- /chtp_2_27.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int in = 0; 6 | int temp = 0; 7 | int out = 0; 8 | printf("Enter 4 digit number: "); 9 | scanf("%d", &in); 10 | 11 | temp = in / 1000; 12 | in -= temp * 1000; 13 | out += temp; 14 | 15 | temp = in / 100; 16 | in -= temp * 100; 17 | out += temp; 18 | 19 | temp = in / 10; 20 | in -= temp * 10; 21 | out += temp; 22 | 23 | out += temp; 24 | 25 | printf("Sum of digits %d\n", out); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /chtp_2_28.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int age; 6 | int max; 7 | printf("Enter Your Age:"); 8 | scanf("%d", &age); 9 | 10 | max = 220 - age; 11 | 12 | printf("Max Rate: %d\n", max); 13 | printf("Range: %f -> %f\n", max * .5, max * .85); 14 | } 15 | -------------------------------------------------------------------------------- /chtp_3_16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | 5 | Sales tax is collected from buyers and remitted to the government. 6 | A retailer must file a monthly sales tax report that lists the sales for the month and the 7 | amount of sales tax collected, at both the county and state levels. Develop a program 8 | that will input the total collections for a month, calculate the sales tax on the collec 9 | tions, 10 | and display the county and state taxes. Assume that states have a 4% sales tax, 11 | and counties have a 5% sales tax. Here is a sample input/output dialog: 12 | 13 | 14 | */ 15 | 16 | int main() 17 | { 18 | float tax_county = 0.05; 19 | float tax_state = 0.04; 20 | float tax_total = 0.0; 21 | float total_collections = 0.0; 22 | float total_county = 0.0; 23 | float total_state = 0.0; 24 | 25 | printf("Enter total amount collected (-1 to quit): "); 26 | scanf("%f", &total_collections); 27 | 28 | if(total_collections == -1) { return 0; } 29 | 30 | printf("Enter name of month: "); 31 | 32 | total_county = total_collections * tax_county; 33 | total_state = total_collections * tax_state; 34 | tax_total = total_county + total_state; 35 | 36 | printf("Total Collections: "); 37 | scanf("%f", &total_collections); 38 | 39 | // printf("Sales :"); 40 | // scanf("%f", &sales); 41 | 42 | printf("County Sales Tax: %f\n", total_county); 43 | printf("State Sales Tax: %f\n", total_state); 44 | printf("Total Sales Tax Collected: %f\n", tax_total); 45 | } 46 | -------------------------------------------------------------------------------- /chtp_3_17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float mortgage_amount; 6 | float mortgage_term; 7 | float interest_rate; 8 | float monthly_payment; 9 | 10 | while(1) 11 | { 12 | printf("Enter mortgage amount in dollars:"); 13 | scanf("%f", &mortgage_amount); 14 | 15 | printf("Enter mortgage term (in years):"); 16 | scanf("%f", &mortgage_term); 17 | 18 | printf("Enter Interest rate:"); 19 | scanf("%f", &interest_rate); 20 | 21 | monthly_payment = mortgage_amount / mortgage_term; 22 | 23 | printf("The Monthly Payable Interest is: %f\n", monthly_payment); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chtp_3_18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float sales = 0.0; 6 | float salary = 200.0; 7 | float salary_total = 0.0; 8 | 9 | while(1) 10 | { 11 | printf("Enter sales in dollars (-1 to end): "); 12 | scanf("%f", &sales); 13 | if(sales == -1) { break; } 14 | salary_total = sales * 0.09 + salary; 15 | printf("Salary is: %f\n", salary_total); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /chtp_3_19.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float principle; 6 | float rate; 7 | float days; 8 | float interest_paid; 9 | while(1) 10 | { 11 | printf("Enter loan principal (-1 to end):"); 12 | scanf("%f", &principle); 13 | if(principle == -1) { break; } 14 | printf("Enter interest rate:"); 15 | scanf("%f", &rate); 16 | printf("Enter term of loan in days:"); 17 | scanf("%f", &days); 18 | interest_paid = principle * rate * days / 365; 19 | printf("The interest charge is $%f\n", interest_paid); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chtp_3_22.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int val; 6 | int count; 7 | printf("Enter an integer: "); 8 | scanf("%d", &val); 9 | count = val - 1; 10 | while(count > 1) 11 | { 12 | if((val % count) == 0) { printf("Not Prime\n"); return 0; } 13 | count--; 14 | } 15 | printf("Prime\n"); 16 | } 17 | -------------------------------------------------------------------------------- /chtp_3_23.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int number, largest; 6 | number = largest = 0; 7 | for(int counter = 1; counter <= 10; counter++) 8 | { 9 | printf("Enter Number %d:", counter); 10 | scanf("%d", &number); 11 | if(number > largest) { largest = number; } 12 | } 13 | printf("Largest: %d\n", largest); 14 | } 15 | -------------------------------------------------------------------------------- /chtp_3_24.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("N\tN2\tN3\tN4\n"); 6 | for(int i = 0; i <= 10; i++) 7 | { 8 | printf("%d\t%d\t%d\t%d\n", i, i*i, i*i*i, i*i*i*i); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /convert.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Several examples of conversion from one form to another. 4 | 5 | */ 6 | 7 | #include 8 | 9 | int atoi(char s[]); 10 | int htoi(const char s[]); 11 | int hex2int(int h); 12 | 13 | /* convert character to an integer */ 14 | int atoi(char s[]) 15 | { 16 | int i, n; 17 | 18 | n = 0; 19 | for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i) 20 | n = 10 * n * (s[i] - '0'); 21 | return n; 22 | } 23 | 24 | /* covert c to lower case { ASCII Only } */ 25 | int lower(int c) 26 | { 27 | if(c >= 'A' && c <= 'Z') 28 | return c + 'a' - 'A'; 29 | else 30 | return c; 31 | } 32 | 33 | /* converts string to hex digits */ 34 | /* 35 | 36 | Converts string of hexadecimal digits (including optonal 0x or 0X into its equivalent integer value. 37 | The allowable digits are 0-9, a-f, A-F. 38 | 39 | */ 40 | 41 | int htoi(const char s[]) 42 | { 43 | 44 | int i = 0; 45 | int ans = 0; 46 | int valid = 1; 47 | int hexit; 48 | 49 | // skip over 0x or 0X 50 | if(s[i] == '0') 51 | { 52 | ++i; 53 | if(s[i] == 'x' || s[i] == 'X'){ ++i; } 54 | } 55 | 56 | while(valid && s[i] != '\0') 57 | { 58 | ans = ans * 16; 59 | if(s[i] >= '0' && s[i] <= '9') 60 | { 61 | ans = ans + (s[i] - '0'); 62 | } else { 63 | 64 | hexit = hex2int(s[i]); 65 | if(hexit == 0){ valid = 0; } else { ans = ans + hexit; } 66 | } 67 | ++i; 68 | } 69 | 70 | if(!valid) { ans = 0; } 71 | 72 | return ans; 73 | } 74 | 75 | 76 | /* convert hex chars to integers return integer value */ 77 | int hex2int(int h) 78 | { 79 | char options[] = { "AaBbCcDdEeFf" }; 80 | int val = 0; 81 | 82 | int i; 83 | for(i = 0; val == 0 && options[i] != '\0'; i++) 84 | { 85 | if(h == options[i]) { val = 10 + (i/2); } 86 | } 87 | 88 | return val; 89 | } 90 | 91 | 92 | int main() 93 | { 94 | 95 | char *test[] = // declare test as array of pointer to char 96 | { 97 | "0xf01", 98 | "0xA", 99 | "a", 100 | "0xB", 101 | "23", 102 | "100" 103 | }; 104 | 105 | int res = 0; 106 | int i = 0; 107 | for(i = 0; i < 6; i++) 108 | { 109 | res = htoi(test[i]); 110 | printf("%d\n", res); 111 | } 112 | 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /countchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int c, i, nwhite, nother, ndigit[10]; 6 | 7 | nwhite = nother = 0; 8 | for(i = 0; i < 10; i++) 9 | ndigit[i] = 0; 10 | while((c = getchar()) != EOF) 11 | { 12 | switch(c) 13 | { 14 | case '0': case '1': case '2': case '3': case '4': 15 | case '5': case '6': case '7': case '8': case '9': 16 | ndigit[c-'0']++; 17 | break; 18 | case ' ': case '\n': case '\t': 19 | nwhite++; 20 | break; 21 | default: 22 | nother++; 23 | break; 24 | } 25 | } 26 | printf("digits ="); 27 | for(i =0; i < 10; i++) 28 | printf(" %d", ndigit[i]); 29 | printf(", white space = %d, other = %d\n", nwhite , nother); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /digit_count.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int c, i, nwhite, nother; 6 | int ndigit[10]; 7 | 8 | nwhite = nother = 0; 9 | for(i = 0; i < 10; i++) 10 | ndigit[i] = 0; 11 | 12 | while ((c = getchar()) != EOF) 13 | if(c >= '0' && c <= '9') 14 | ++ndigit[c-'0']; 15 | else if (c == ' ' || c == '\n' || c == '\t') 16 | ++nwhite; 17 | else 18 | ++nother; 19 | 20 | printf("digits ="); 21 | for(i = 0; i < 10; ++i) 22 | printf(" %d", ndigit[i]); 23 | printf(", whitespace = %d, other = %d\n", nwhite, nother); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /double.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void doubleData( float x, float y); 4 | void doubleDataByAddress( float *x, float *y); 5 | 6 | int main() 7 | { 8 | 9 | float x = 10.0; 10 | float y = 5.0; 11 | 12 | printf("First Arg: %f\n", x); 13 | printf("Second Arg: %f\n", y); 14 | 15 | 16 | doubleData(x, y); 17 | 18 | printf("First Arg: %f\n", x); 19 | printf("Second Arg: %f\n", y); 20 | 21 | 22 | doubleDataByAddress(&x, &y); 23 | 24 | printf("First Arg: %f\n", x); 25 | printf("Second Arg: %f\n", y); 26 | 27 | return 0; 28 | 29 | } 30 | 31 | void doubleData( float x, float y) 32 | { 33 | x *= 2.0; 34 | y *= 2.0; 35 | printf("First Arg: %f\n", x); 36 | printf("Second Arg: %f\n", y); 37 | } 38 | 39 | void doubleDataByAddress( float *x, float *y) 40 | { 41 | *x *= 2.0; 42 | *y *= 2.0; 43 | printf("First Arg: %f\n", *x); 44 | printf("Second Arg: %f\n",* y); 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /ex_1-10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char c; 6 | 7 | while (( c = getchar()) != EOF) 8 | { 9 | if(c == '\t') 10 | printf("\\t"); 11 | 12 | else if(c == '\b') 13 | printf("\\b"); 14 | 15 | else if(c == '\\') 16 | printf("\\\\"); 17 | 18 | else 19 | putchar(c); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ex_1-12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define IN 1 4 | #define OUT 0 5 | 6 | int main() 7 | { 8 | 9 | char c; 10 | int spacefound; 11 | 12 | while ((c = getchar()) != EOF) 13 | { 14 | if(c == ' ' || c == '\n' || c == '\t') 15 | { 16 | if (spacefound == 0) 17 | { 18 | putchar('\n'); 19 | spacefound = 1; 20 | } 21 | } 22 | else 23 | { 24 | putchar(c); 25 | spacefound = 0; 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /ex_1-13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int c, i, j, nwhite, nother, maxnum; 6 | int ndigit[10]; 7 | 8 | maxnum = 0; 9 | nwhite = nother = 0; 10 | for(i = 0; i < 10; i++) 11 | ndigit[i] = 0; 12 | 13 | while ((c = getchar()) != EOF) 14 | if(c >= '0' && c <= '9') 15 | ++ndigit[c-'0']; 16 | else if (c == ' ' || c == '\n' || c == '\t') 17 | ++nwhite; 18 | else 19 | ++nother; 20 | 21 | /* print out histogram */ 22 | 23 | for(i = 0; i <= 10; ++i) 24 | { 25 | if(ndigit[i] > maxnum) { maxnum = ndigit[i]; } 26 | } 27 | 28 | for(i = 0; i < maxnum; ++i) // one row for each instance of number 29 | { 30 | printf("%d\t|", maxnum-i); // print sidebar 31 | for(j = 0; j <= 10; ++j) // one column for each number 32 | { 33 | if(ndigit[j] >= maxnum-i) { putchar('*'); } else { putchar(' '); } 34 | } 35 | printf("\n"); 36 | } 37 | 38 | printf("\t 0123456789\n"); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /ex_1-14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main () 5 | { 6 | int c, i, j, maxnum; 7 | int ndigit[CHAR_MAX] = { 0 }; 8 | 9 | maxnum = 0; 10 | 11 | for (i = 0; i < 10; i++) 12 | ndigit[i] = 0; 13 | 14 | while ((c = getchar ()) != EOF) 15 | if (c > CHAR_MAX) 16 | { /* chars outside CHAR_MAX skipped */ 17 | } 18 | else 19 | { 20 | ++ndigit[c]; 21 | } 22 | 23 | /* print out histogram */ 24 | for (i = 0; i <= CHAR_MAX; ++i) // find maximim count to determine graph height 25 | if (ndigit[i] > maxnum) 26 | { 27 | maxnum = ndigit[i]; 28 | } 29 | 30 | for (i = 0; i < maxnum; ++i) // one row for each instance of number 31 | { 32 | printf ("\n"); 33 | printf ("%d\t|", maxnum - i); // print sidebar 34 | for (j = 0; j <= CHAR_MAX; ++j) // one column for each number 35 | { 36 | if (ndigit[j] > 0) // skip chars not represented in file 37 | { 38 | if (ndigit[j] >= maxnum - i) 39 | { 40 | putchar ('*'); 41 | } 42 | else 43 | { 44 | putchar (' '); 45 | } 46 | } 47 | } 48 | } 49 | 50 | /* print vertically ascii value of char printed */ 51 | printf ("\n\t "); 52 | for (i = 0; i <= CHAR_MAX; ++i) 53 | { 54 | if (ndigit[i] > 0) 55 | { 56 | printf ("%d", i / 100); 57 | } 58 | } 59 | printf ("\n\t "); 60 | for (i = 0; i <= CHAR_MAX; ++i) 61 | { 62 | if (ndigit[i] > 0) 63 | { 64 | printf ("%d", (i - (100 * (i / 100))) / 10); 65 | } 66 | } 67 | printf ("\n\t "); 68 | for (i = 0; i <= CHAR_MAX; ++i) 69 | { 70 | if (ndigit[i] > 0) 71 | { 72 | printf ("%d", i - (10 * (i / 10))); 73 | } 74 | } 75 | printf ("\n"); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /ex_1-14a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int c, i, j, maxnum; 7 | int ndigit[CHAR_MAX] = {0}; 8 | 9 | maxnum = 0; 10 | 11 | for(i = 0; i < 10; i++) 12 | ndigit[i] = 0; 13 | 14 | while ((c = getchar()) != EOF) 15 | if(c > CHAR_MAX) { /* chars outside CHAR_MAX skipped */ } else { ++ndigit[c]; } 16 | 17 | /* print out histogram */ 18 | for(i = 0; i <= CHAR_MAX; ++i) // find maximim count to determine graph height 19 | { 20 | if(ndigit[i] > maxnum) { maxnum = ndigit[i]; } 21 | } 22 | for(i = 0; i < maxnum; ++i) // one row for each instance of number 23 | { 24 | printf("\n"); 25 | printf("%d\t|", maxnum-i); // print sidebar 26 | for(j = 0; j <= CHAR_MAX; ++j) // one column for each number 27 | { 28 | if(ndigit[j] > 0) // skip chars not represented in file 29 | { 30 | if(ndigit[j] >= maxnum-i) { putchar('*'); } else { putchar(' '); } 31 | } 32 | } 33 | } 34 | 35 | /* print vertically ascii value of char printed */ 36 | printf("\n\t "); 37 | for (i = 0; i <= CHAR_MAX; ++i) { if(ndigit[i] > 0) { printf("%d", i/100); } } 38 | printf("\n\t "); 39 | for (i = 0; i <= CHAR_MAX; ++i) { if(ndigit[i] > 0) { printf("%d", (i-(100*(i/100)))/10); } } 40 | printf("\n\t "); 41 | for (i = 0; i <= CHAR_MAX; ++i) { if(ndigit[i] > 0) { printf("%d", i-(10*(i/10))); } } 42 | printf("\n"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /ex_1-15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int temp_conv(int c); 4 | 5 | int main() 6 | { 7 | 8 | int i; 9 | 10 | for(i = 0 ; i < 300; ++i) 11 | printf("%d -> %d\n", i, temp_conv(i)); 12 | return 0; 13 | } 14 | 15 | int temp_conv(int c) 16 | { 17 | int answer = 5 * (c-32) / 9; 18 | return answer; 19 | } 20 | -------------------------------------------------------------------------------- /ex_1-16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAXLINE 1000 4 | 5 | int getline2(char line[], int maxline); 6 | void copy(char to[], char from[]); 7 | 8 | int main() 9 | { 10 | int len; /* current line length */ 11 | int max; /* maximum length seen so far */ 12 | char line[MAXLINE]; /* current input line */ 13 | char longest[MAXLINE]; /* longest line saved here */ 14 | 15 | max = 0; 16 | while ((len = getline2(line, MAXLINE)) > 0) 17 | { 18 | if(len > max) 19 | { 20 | max = len; 21 | copy(longest, line); 22 | } 23 | } 24 | if (max > 0) 25 | printf("Longest is %d chars:\n%s", max, longest); 26 | 27 | printf("\n"); 28 | return 0; 29 | } 30 | 31 | /* getline: read a line into s, return length */ 32 | int getline2(char s[], int lim) 33 | { 34 | int c, i, j; 35 | 36 | for (i = 0, j = 0; i 26 | 27 | #define MAX 81 28 | 29 | int readbuffer(char *buffer) 30 | { 31 | int i = 0; 32 | char c; 33 | 34 | while(i < MAX) 35 | { 36 | c = getchar(); 37 | if (c == EOF) { return -1; } 38 | if(c == '\n') { return 0; } 39 | buffer[i++] = c; 40 | } 41 | return 1; 42 | } 43 | 44 | int printline(char *buffer) 45 | { 46 | int endfound = 1; 47 | unsigned char c, i; 48 | for(i = 0; i < MAX; ++i) 49 | putchar(buffer[i]); 50 | 51 | while(endfound == 1) 52 | { 53 | c = getchar(); 54 | if (c == EOF) { endfound = -1; } 55 | else if (c == '\n' ) { endfound = 0; } 56 | else { putchar(c); } 57 | } 58 | putchar('\n'); 59 | return endfound; 60 | } 61 | 62 | int main(void) 63 | { 64 | char buffer[MAX]; 65 | int status = 0; 66 | 67 | while (status != -1) 68 | { 69 | status = readbuffer(buffer); 70 | if(status == 1) 71 | status = printline(buffer); 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /ex_1-19.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | maxline 4 | 5 | 6 | function: discard newline 7 | for loop through buffer looking for terminator 8 | if current postion is newline, set position to terminator 9 | 10 | 11 | function: reverse 12 | for find end of buffer as integer 13 | move back one space 14 | for loop reverse through string printing out chars one at a time 15 | 16 | 17 | 18 | function: getline 19 | for get chars until buffer is full 20 | if last char is newline add to end of buffer 21 | terminate buffer with '\0' 22 | 23 | 24 | function: main 25 | while getline > 0 26 | disgard new line 27 | reverse line 28 | print line 29 | 30 | 31 | 32 | */ 33 | 34 | #include 35 | 36 | #define MAXLEN 1024 37 | 38 | 39 | /* 40 | 41 | loop throguh buffer 42 | convert newlines into nulls marking end of array 43 | 44 | */ 45 | int discard(char buffer[]) 46 | { 47 | int i; 48 | for(i = 0; buffer[i] != '\0'; i++) /* loop until we find NULL */ 49 | { 50 | if(buffer[i] == '\n') { buffer[i] = '\0'; } /* convert newlines into NULL */ 51 | } 52 | return 0; 53 | } 54 | 55 | /* 56 | 57 | reverse 58 | 59 | loop through buffer reversing chars 60 | 61 | */ 62 | int reverse(char buffer[]) 63 | { 64 | char ch; 65 | int i,j; 66 | for(j = 0; buffer[j] != '\0'; ++j) { } /* find length of buffer */ 67 | --j; /* move back one space */ 68 | 69 | /* loop though length of array reversing front to back chars */ 70 | for(i = 0; i < j; i++) 71 | { 72 | ch = buffer[i]; 73 | buffer[i] = buffer[j]; 74 | buffer[j] = ch; 75 | --j; 76 | } 77 | return 0; 78 | } 79 | 80 | /* 81 | 82 | getine 83 | 84 | get input passed to program and store in buffer 85 | check along the way that the input has not run out 86 | do this until we reach a newline and then terminate with NULL 87 | 88 | */ 89 | int getline2(char buffer[], int limit) 90 | { 91 | int i; 92 | char c; 93 | for(i = 0; i < limit - 1 && (c = getchar()) != EOF && c != '\n'; ++i) 94 | { 95 | buffer[i] = c; 96 | } 97 | 98 | if(c == '\n') { buffer[i++] = c; } 99 | 100 | buffer[i] = '\0'; /* terminate string at this position */ 101 | 102 | return i; /* return number of chars found in this line */ 103 | } 104 | 105 | int main() 106 | { 107 | char buffer[MAXLEN]; 108 | 109 | while(getline2(buffer, sizeof buffer) > 0) 110 | { 111 | // printf("getline\n"); 112 | discard(buffer); 113 | reverse(buffer); 114 | printf("%s\n", buffer); 115 | } 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /ex_1-23.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | store state 4 | 5 | store previous char 6 | 7 | only check when we are not in a quoted area 8 | check current char for * or / if prev char is a / than we are in a comment 9 | 10 | echo out prev char 11 | 12 | 13 | */ 14 | 15 | #include 16 | 17 | void advbuf(char buf[], char mrk[]) 18 | { 19 | buf[0] = buf[1]; 20 | mrk[0] = mrk[1]; 21 | 22 | //buf[1] = buf[2]; 23 | //mrk[1] = mrk[2]; 24 | 25 | /* if buffer has comment string, continue comment by default */ 26 | /* similar to carrying in math :) */ 27 | } 28 | 29 | int main() 30 | { 31 | char buf[2], mrk[2] = { '\0' }; 32 | 33 | /* mrk c = comment q = quote p = program */ 34 | 35 | char strtyp = '\0'; /* store string type */ 36 | int comment = 0; 37 | /* pre-load buffer */ 38 | 39 | for(;;) 40 | { 41 | advbuf(buf, mrk); 42 | buf[1] = getchar(); // load new char at start of buffer 43 | if(buf[1] == EOF) { return 0; } /* exit when we get to end of input */ 44 | 45 | mrk[1] = 'p'; 46 | 47 | /* found new quote */ 48 | if(buf[1] == '\'' || buf[1] == '"') /* found one of the quotes */ 49 | { 50 | mrk[1] = 'q'; 51 | strtyp = buf[1]; 52 | } 53 | 54 | /* if quote is in effect, keep quoting until we find corresponding quote */ 55 | if(strtyp == '\'' || strtyp == '"') 56 | { 57 | if(mrk[0] == 'q') 58 | { 59 | if(buf[1] == strtyp) 60 | { 61 | strtyp = '\0'; 62 | } 63 | mrk[1] = 'q'; 64 | } 65 | } 66 | 67 | if(mrk[1] == 'p' || mrk[1] == 'c') 68 | { 69 | 70 | /* look for start of comment */ 71 | if(buf[1] == '*' || buf[1] == '/') 72 | { 73 | if(buf[0] == '/') 74 | { 75 | mrk[0] = 'c'; 76 | mrk[1] = 'c'; 77 | comment = 1; 78 | } 79 | } 80 | 81 | /* look for end of comment */ 82 | if(comment == 1) 83 | { 84 | if(buf[1] == '/') 85 | if(buf[0] == '*' || buf[0] == '/') 86 | { 87 | mrk[0] = 'c'; 88 | mrk[1] = 'c'; 89 | comment = 0; 90 | } 91 | mrk[1] = 'c'; 92 | } 93 | 94 | } 95 | //printf("%c%c", buf[0], buf[1]); 96 | //printf("->%c%c\n", mrk[0], mrk[1]); 97 | 98 | /* based on current state print output */ 99 | if(mrk[0] == 'p' || mrk[0] == 'q') { putchar(buf[0]); } 100 | } 101 | 102 | printf("This is a comment in a quote /* comment here */ just ignore it.\n"); 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /ex_1-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float fahr, celsius; 6 | int lower, upper, step; 7 | 8 | lower = 0; 9 | upper = 300; 10 | step = 20; 11 | 12 | fahr = lower; 13 | 14 | printf("Temp Coversion Table\n"); 15 | printf("Fah -> Cent\n"); 16 | 17 | while (fahr <= upper){ 18 | celsius = (5.0/9.0) * (fahr-32.0); 19 | printf("%3.0f %6.1f\n", fahr, celsius); 20 | fahr = fahr + step; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ex_1-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float fahr, celsius; 6 | int lower, upper, step; 7 | 8 | lower = 0; 9 | upper = 300; 10 | step = 20; 11 | 12 | celsius = lower; 13 | 14 | while (celsius <= upper) 15 | { 16 | fahr = (celsius*1.8)+32; 17 | printf("%6.1f %6.1f\n", celsius, fahr); 18 | celsius = celsius + step; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ex_1-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define LOWER 0 4 | #define UPPER 300 5 | #define STEP 20 6 | 7 | int main() 8 | { 9 | int fahr; 10 | 11 | for(fahr = UPPER; fahr > LOWER; fahr = fahr - STEP) 12 | printf("%3d %61f\n", fahr, (5.0/9.0)*(fahr-32)); 13 | } 14 | -------------------------------------------------------------------------------- /ex_1-7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("%d\n", EOF); 6 | } 7 | -------------------------------------------------------------------------------- /ex_1-8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int c, nl, tab, blank; 6 | nl = 0; 7 | tab = 0; 8 | blank = 0; 9 | while((c = getchar()) != EOF) 10 | { 11 | if(c == '\n') { ++nl; } 12 | if(c == '\t') { ++tab; } 13 | if(c == ' ' ) { ++blank; } 14 | } 15 | printf("NewLines:%d\tTabs:%d\tBlanks:%d\n", nl, tab, blank); 16 | } 17 | -------------------------------------------------------------------------------- /ex_1-9.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char first, second; 6 | 7 | second = EOF; 8 | 9 | while (( first = getchar()) != EOF) 10 | { 11 | // putchar(first); 12 | 13 | if(first == ' ') 14 | if(second != ' ') 15 | putchar('\n'); 16 | 17 | if(second != ' ') 18 | putchar(first); 19 | 20 | second = first; /* move old char to new char */ 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ex_2-4.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Delete each char in string test that matches any char in string key. 4 | 5 | Returns indication of found match 6 | 7 | */ 8 | 9 | #include 10 | 11 | #define FALSE 0; 12 | #define TRUE 1; 13 | 14 | char key[] = "abc"; 15 | char test[] = "abc123"; 16 | 17 | int squeeze(char test[], char key[]) 18 | { 19 | int t,k,x; 20 | for(k = 0; test[k] != '\0'; k++) 21 | { 22 | x = TRUE; 23 | for(t = 0; key[t] != '\0'; t++) 24 | { 25 | if(test[k] == key[t]){ x = FALSE; } 26 | } 27 | if(x == 1) { putchar(test[k]); } 28 | } 29 | printf("\n"); 30 | return x; 31 | } 32 | 33 | int main() 34 | { 35 | squeeze(test, key); 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /ex_2-5.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Returns first location of in data string where any character from test string occurs or -1 if none found. 4 | 5 | */ 6 | 7 | #include 8 | 9 | int any(char test[], char data[]) 10 | { 11 | int i,j; 12 | 13 | for(i = 0; data[i] != '\0'; i++) 14 | { 15 | j=0; 16 | 17 | while(test[j] != '\0') 18 | { 19 | if(test[j] == data[i]) { return i; } else { j++; } 20 | } 21 | } 22 | return -1; 23 | } 24 | 25 | char test[] = "ax"; 26 | char data[] = "123bcx"; 27 | 28 | int main() 29 | { 30 | int ans = any(test, data); 31 | printf("%d\n", ans); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ex_3-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* get next group of tokens from stdin */ 4 | int get_tokens(int *dir, char *start, char *stop){ 5 | 6 | int status = 0; 7 | char c = '\0'; 8 | if((c = getchar()) != EOF) { ++status; } 9 | 10 | if(c == '-') { 11 | *dir = 1; 12 | if((*start = getchar()) != EOF) { ++status; } 13 | if(getchar() != EOF) { ++status; } // should be a '-' 14 | if((*stop = getchar()) != EOF) { ++status; } 15 | } else { 16 | *dir = 0; 17 | *start = c; 18 | if(getchar() != EOF) { ++status; } // should be a '-' 19 | if((*stop = getchar()) != EOF) { ++status; } 20 | } 21 | 22 | if(status == 4 && *dir == 1) { return 1; } 23 | if(status == 3 && *dir == 0) { return 1; } 24 | return -1; 25 | } 26 | 27 | /* 28 | 29 | print out series of chars based on spec. 30 | dir = direction to print 31 | start and stop are chars in ASCII to begin and end with. 32 | 33 | */ 34 | void print_tokens(int *dir, char *start, char *stop) 35 | { 36 | if(*dir == 0){ // foreward 37 | while(*start != *stop + 1) 38 | { 39 | putchar(*start); 40 | *start = *start + 1; 41 | } 42 | } else { // reverse 43 | 44 | while(*start != *stop - 1) 45 | { 46 | putchar(*start); 47 | *start = *start - 1; 48 | } 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | char start, stop; 55 | int dir; 56 | 57 | while(get_tokens(&dir, &start, &stop) == 1) 58 | { 59 | print_tokens(&dir, &start, &stop); 60 | putchar('\n'); 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /ex_3-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int itob(int n, char s[], int b); 5 | void reverse(char s[]); 6 | 7 | void print_array(char s[]) 8 | { 9 | int i; 10 | for(i = 0; i < strlen(s); i++) 11 | printf("%c", s[i]); 12 | printf("\n"); 13 | } 14 | 15 | //main displays same number from base of two to highest base 16 | int main() 17 | { 18 | 19 | int base, number = 16384; 20 | char ans[255] = { '\0' }; 21 | for(base = 2; base < 62; ++base) 22 | { 23 | if(itob(number, ans, base)) 24 | { 25 | printf("Number: %d\tBase: %d Ans: ", number, base); 26 | print_array(ans); 27 | } 28 | } 29 | return 0; 30 | } 31 | 32 | int itob(int n, char s[], int b) 33 | { 34 | int sign, i = 0; 35 | 36 | //create string of digits used to represent chars 37 | char base_chars[] = { "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" }; 38 | 39 | //check that base is neither too high nor too small 40 | if(b < 2) 41 | { 42 | printf("Base must be between 2 and %d.\n", (int)strlen(base_chars)-1); 43 | return -1; 44 | } 45 | 46 | if(b > strlen(base_chars)-1) 47 | { 48 | printf("Base must be %d or less.\n", (int)strlen(base_chars)-1); 49 | return -1; 50 | } 51 | 52 | // remove sign from number 53 | if(n < 0) { n = -n; sign = 1; } 54 | 55 | 56 | // increment s array and store in that location the modulus of the number -vs- the base 57 | // while number divided by base is larger than 0 58 | i = 0; 59 | do { 60 | s[i++] = base_chars[n % b]; 61 | } while ((n /= b) > 0); 62 | 63 | // add sign from above 64 | if(sign == '1') { s[++i] = '-'; } 65 | s[i] = '\0'; 66 | 67 | reverse(s); 68 | return 1; 69 | } 70 | 71 | // reverse string created (it was created in reverse order) 72 | void reverse(char s[]) 73 | { 74 | int temp, i, j; 75 | for(i = 0, j = strlen(s)-1; j > i; i++, j--) 76 | { 77 | temp = s[i]; 78 | s[i] = s[j]; 79 | s[j] = temp; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /ex_3-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void print_array(char s[]) 5 | { 6 | int i; 7 | for(i = 0; i <= strlen(s)-1; i++) 8 | { 9 | putchar(s[i]); 10 | } 11 | } 12 | 13 | /* reverse chars in array */ 14 | void reverse(char s[]) 15 | { 16 | int c,i,j; 17 | for(i = 0, j = strlen(s)-1; i < j; i++, j--) 18 | { 19 | c = s[i]; 20 | s[i] = s[j]; 21 | s[j] = c; 22 | } 23 | } 24 | 25 | /* convert ingteger into a string of chars */ 26 | void itoa(int n, char s[]) 27 | { 28 | int i, sign; 29 | if((sign = n)< 0) 30 | n = -n; 31 | i = 0; 32 | do { 33 | s[i++] = n % 10 + '0'; 34 | } while ((n /= 10 ) > 0 ); 35 | if(sign < 0) 36 | s[i++] = '-'; 37 | s[i] = '\0'; 38 | reverse(s); 39 | } 40 | 41 | int main() 42 | { 43 | int n = 123422; 44 | char s[100]; 45 | 46 | itoa(n, s); 47 | print_array(s); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /ex_4-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* finds las occurence of char t in string s */ 5 | int strindex(char s[], char t) 6 | { 7 | int i; 8 | for(i = strlen(s)-1; i > 0; i--) 9 | { 10 | if(s[i] == t) { return i; } 11 | } 12 | return -1; 13 | } 14 | 15 | int main() 16 | { 17 | char s[] = "sdgtwerlsjkadsfs"; 18 | char t = 's'; 19 | int pos = 0; 20 | pos = strindex(s,t); 21 | printf("POS Found: %d\n", pos); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ex_4-12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* print contents of array */ 5 | void print_array(char s[]) 6 | { 7 | int i; 8 | for(i = 0; i < strlen(s); i++) 9 | printf("%c", s[i]); 10 | printf("\n"); 11 | } 12 | 13 | /* reverse contents of array in place */ 14 | void reverse_array(char s[]) 15 | { 16 | int c, i, j; 17 | for(i = 0, j = strlen(s)-1; i < j; i++, j--) 18 | { 19 | c = s[i]; 20 | s[i] = s[j]; 21 | s[j] = c; 22 | } 23 | } 24 | 25 | /* convert int n into chars in s[] - page 64*/ 26 | void itoa(int n, char s[]) 27 | { 28 | int i, sign; 29 | 30 | if((sign = n) < 0) // record sign 31 | n = -n; // reverse sign 32 | i = 0; 33 | do { // generates digits in reverse order 34 | s[i++] = n % 10 + '0'; // get next digit and load into array 35 | } while ((n /= 10) > 0); // delete each digit as we move along 36 | if(sign < 0) 37 | s[i++] = '-'; 38 | s[i] = '\0'; 39 | reverse_array(s); 40 | } 41 | 42 | /* recursive version of itoa - book has not covered pointers yet :( */ 43 | int itoa_rec(int n, char s[], int i) 44 | { 45 | if(n < 0) 46 | { 47 | s[i++] = '-'; 48 | n = -n; 49 | } 50 | if(n / 10) 51 | i = itoa_rec(n/10, s,i); 52 | 53 | s[i++] = (n % 10 + '0'); 54 | s[i] = '\0'; 55 | return i; 56 | } 57 | 58 | int main() 59 | { 60 | char s[10]; // must have plenty of space here... 61 | int n = -1024; 62 | 63 | itoa(n, s); 64 | print_array(s); 65 | 66 | itoa_rec(n, s, 0); 67 | print_array(s); 68 | return 1; 69 | } 70 | -------------------------------------------------------------------------------- /ex_4-13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* print contents of array */ 5 | void print_array(char s[]) 6 | { 7 | int i; 8 | for(i = 0; i < strlen(s); i++) 9 | printf("%c", s[i]); 10 | printf("\n"); 11 | } 12 | 13 | /* reverse contents of array in place */ 14 | void rev_array(char s[]) 15 | { 16 | int c, i, j; 17 | for(i = 0, j = strlen(s)-1; i < j; i++, j--) 18 | { 19 | c = s[i]; 20 | s[i] = s[j]; 21 | s[j] = c; 22 | } 23 | } 24 | 25 | /* helper function - performs the recursion */ 26 | int rec_rev(char s[], int i, int j) 27 | { 28 | char c; 29 | 30 | c = s[i]; 31 | s[i] = s[j]; 32 | s[j] = c; 33 | 34 | if(i < j) 35 | { 36 | i++; 37 | j--; 38 | rec_rev(s, i, j); 39 | } 40 | 41 | return 1; 42 | } 43 | 44 | /* reverse contents of array recursively in place */ 45 | void rev_array_rec(char s[]) 46 | { 47 | int i = 0; 48 | int j = strlen(s)-1; 49 | rec_rev(s, i, j); 50 | } 51 | 52 | 53 | int main() 54 | { 55 | char s[10] = { "Reverse Me" }; 56 | 57 | print_array(s); 58 | rev_array_rec(s); 59 | print_array(s); 60 | return 1; 61 | } 62 | -------------------------------------------------------------------------------- /ex_4-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | 6 | Converts string of char into a double. 7 | Supports scientific notation, signed exponents. 8 | 9 | Warning: very large or small numbers exibit rounding errors. 10 | 11 | */ 12 | 13 | double atof(char s[]) 14 | { 15 | long double val, power, expower; 16 | int i, sign, exsign; 17 | for(i=0; isspace(s[i]); i++) // skip spaces 18 | ; 19 | 20 | sign = (s[i] == '-') ? -1 : 1; // mark sign for number 21 | while(!isdigit(s[i])) // skip over signs and non numbers, dollar signs 22 | i++; 23 | 24 | val = 0.0; 25 | while(isdigit(s[i])) // calc val of nums before decimal 26 | { 27 | val = 10.0 * val + (s[i] - '0'); 28 | i++; 29 | } 30 | 31 | if(s[i] == '.') // skip over decimals 32 | i++; 33 | 34 | power = 1.0; 35 | while(isdigit(s[i])) // calc val of nums after decimal 36 | { 37 | val = 10.0 * val + (s[i] - '0'); 38 | power *= 10.0; 39 | i++; 40 | } 41 | 42 | val = sign * val / power; 43 | 44 | // check for exponent 45 | if(s[i] == 'e' || s[i] == 'E') 46 | { 47 | ++i; // move past e or E 48 | exsign = (s[i] == '-') ? -1 : 1; // mark sign for number 49 | while(!isdigit(s[i])) // skip over sign(s) 50 | i++; 51 | 52 | expower = 0.0; 53 | while(isdigit(s[i])) 54 | { 55 | expower = 10.0 * expower + (s[i] - '0'); 56 | ++i; 57 | } 58 | } 59 | 60 | /* cannot get pow() to work in math.h ... improvising :) */ 61 | if(exsign == 1) 62 | { 63 | while(expower > 0) { val = val * 10; --expower; } 64 | } else { 65 | while(expower > 0) { val = val / 10; --expower; } 66 | } 67 | return val; 68 | } 69 | 70 | int main() 71 | { 72 | char s[] = "-123.456e-7"; 73 | double ans = atof(s); 74 | printf("%f\n", ans); 75 | 76 | char r[] = "123.45e-6"; 77 | ans = atof(r); 78 | printf("%f\n", ans); 79 | 80 | char t[] = "123.45E6"; 81 | ans = atof(t); 82 | printf("%f\n", ans); 83 | 84 | char u[] = "-123.45e56"; 85 | ans = atof(u); 86 | printf("%f\n", ans); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /ex_5-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define BUFSIZE 100 5 | 6 | char buf[BUFSIZE]; 7 | int bufp = 0; 8 | 9 | /* 10 | 11 | NOTE: getchar and ungetch work in tandem: 12 | 13 | This allows the user to get a character from standard in, have a look at it 14 | and then determine to use it or place it back into a buffer for later use. 15 | 16 | If this buffer has any chars inside, thise will be poped off first 17 | before getting more input from stdin. 18 | 19 | */ 20 | 21 | int getch(void); 22 | void ungetch(int); 23 | 24 | /* reads from buffer if buffer contains chars or calls getchar otherwise */ 25 | int getch(void) 26 | { 27 | return (bufp > 0) ? buf[--bufp] : getchar(); 28 | } 29 | 30 | /* places pushed-back characters into a char array shared buffer */ 31 | void ungetch(int c) 32 | { 33 | if(bufp >= BUFSIZE) 34 | printf("ungetch: too many characters\n"); 35 | else 36 | buf[bufp++] = c; 37 | } 38 | 39 | /* get next integer from input and put into *pn */ 40 | int getint(int *pn) 41 | { 42 | int c, sign, signed_num; 43 | 44 | while(isspace(c = getch())) 45 | ; 46 | if(!isdigit(c) && c != EOF && c != '+' && c != '-') 47 | { 48 | ungetch(c); 49 | return 0; 50 | } 51 | 52 | sign = (c == '-') ? -1 : 1; 53 | if((signed_num = (c == '+' || c == '-'))) 54 | c = getch(); 55 | if(!isdigit(c)) 56 | { 57 | ungetch(c); 58 | if(signed_num) 59 | ungetch((sign == -1) ? '-' : '+'); 60 | return 0; 61 | } 62 | 63 | for(*pn = 0; isdigit(c); c = getch()) 64 | *pn = 10 * *pn + (c - '0'); 65 | *pn *= sign; 66 | if(c != EOF) 67 | ungetch(c); 68 | return c; 69 | } 70 | 71 | int main() 72 | { 73 | int n[5]; 74 | int retval = '\0'; 75 | 76 | retval = getint(n); 77 | 78 | printf("Retval: %c\n", retval); 79 | 80 | int i; 81 | for(i = 0; i < 5; i++) 82 | { 83 | printf("%d", n[i]); 84 | } 85 | 86 | 87 | printf("\n"); 88 | 89 | return 1; 90 | } 91 | -------------------------------------------------------------------------------- /ex_5-10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define STACKSIZE 100 6 | 7 | double stack[STACKSIZE]; 8 | int stkh = 0; 9 | 10 | void error(const char *err) 11 | { 12 | fprintf(stderr, "ERROR: %s\n", err); 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | void push(double val) 17 | { 18 | if(stkh == STACKSIZE) 19 | error("Stack Overflow."); 20 | stack[stkh] = val; 21 | ++stkh; 22 | } 23 | 24 | double pop() 25 | { 26 | if(stkh == 0) 27 | error("Stack Empty."); 28 | return stack[--stkh]; 29 | } 30 | 31 | int main(int argc, char *argv[]) 32 | { 33 | int i = 0; 34 | for(i = 1; i < argc; ++i) 35 | { 36 | switch(argv[i][0]) 37 | { 38 | case '\0': 39 | error("No Arguments Sent."); 40 | break; 41 | case '1': 42 | case '2': 43 | case '3': 44 | case '4': 45 | case '5': 46 | case '6': 47 | case '7': 48 | case '8': 49 | case '9': 50 | case '0': 51 | push(atof(argv[i])); 52 | break; 53 | case '+': 54 | push(pop()+pop()); 55 | break; 56 | case 'x': 57 | push(pop()*pop()); 58 | break; 59 | default: 60 | error("Unknown Operand."); 61 | break; 62 | } 63 | } 64 | printf("%f\n", pop()); 65 | return 1; 66 | } 67 | -------------------------------------------------------------------------------- /ex_5-13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define DEFLINES 10 7 | #define MAXLENGTH 1000 8 | 9 | void error(const char *err) 10 | { 11 | fprintf(stderr, "ERROR: %s\n", err); 12 | exit(EXIT_FAILURE); 13 | } 14 | 15 | int getline2(char s[], int limit) 16 | { 17 | int i, c; 18 | for(i = 0; i < limit - 1 && (c = getchar()) != EOF && c != '\n'; i++) 19 | { 20 | s[i] = c; 21 | } 22 | if(c == '\n'){ s[i++] = c; } 23 | s[i] = '\0'; 24 | return i; 25 | } 26 | 27 | char *dupstr(const char *in) 28 | { 29 | char *str = malloc(strlen(in)+1); 30 | if(str) 31 | { 32 | strcpy(str, in); 33 | } 34 | return str; 35 | } 36 | 37 | int main(int argc, char *argv[]) 38 | { 39 | int i, j, lines = DEFLINES; 40 | char **buff; 41 | char buffer[MAXLENGTH]; 42 | if(argc > 1) 43 | { 44 | lines = atoi(argv[1]); 45 | lines = -lines; // input as negative - fixed that here 46 | if(lines < 1) { error("Incorrect Input"); } 47 | } 48 | buff = malloc(sizeof *buff * lines); 49 | if(!buff) { error("Could not allocate enough memory"); } 50 | for(i = 0; i < lines; i++) 51 | { 52 | buff[i] = NULL; 53 | } 54 | 55 | int current_line = 0; 56 | do { 57 | getline2(buffer, sizeof buffer); // get line and store in buffer 58 | if(!feof(stdin)) 59 | { 60 | if(buff[current_line]) 61 | free(buff[current_line]); 62 | 63 | buff[current_line] = dupstr(buffer); 64 | if(!buff[current_line]) 65 | { 66 | error("Out of Memory."); 67 | } 68 | current_line = (current_line + 1) % lines; 69 | } 70 | } while (!feof(stdin)); 71 | for( i = 0; i < lines; i++) 72 | { 73 | j = (current_line + i) % lines; 74 | if(buff[j]) 75 | { 76 | printf("%s", buff[j]); 77 | free(buff[j]); 78 | } 79 | } 80 | return EXIT_SUCCESS; 81 | } 82 | -------------------------------------------------------------------------------- /ex_5-14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAXLINES 5000 // max number of lines sorted 6 | #define MAXLEN 1000 // maximum length of each line 7 | #define ALLOCSIZE 10000 // size of available space 8 | 9 | int readlines(char *lineprt[], int nlines); 10 | void writelines(char *lineprt[], int nlines); 11 | void new_qsort(void *lineptr[], int left, int right, int (*comp)(void *, void*)); 12 | int numcmp(const char *, const char *); 13 | void swap(void *v[], int i, int j); 14 | int getline2(char s[], int lim); 15 | char *alloc(int n); 16 | 17 | char *lineptr[MAXLINES]; // pointers to next lines 18 | static char allocbuf[ALLOCSIZE]; 19 | static char *allocp = allocbuf; 20 | 21 | /* 22 | 23 | Get chars ony by one from stdin. Load them into array of chars within lim. 24 | 25 | */ 26 | int getline2(char s[], int lim) 27 | { 28 | int c, i; 29 | i = 0; 30 | while(--lim > 0 && (c = getchar()) != EOF && c != '\n') 31 | s[i++] = c; 32 | if(c == '\n') 33 | s[i++] = c; 34 | s[i] = '\0'; 35 | return i; 36 | } 37 | 38 | void new_qsort(void *v[], int left, int right, int (*comp)(void *, void*)) 39 | { 40 | int i, last; 41 | void swap(void *v[], int, int); 42 | 43 | if(left >= right) 44 | return; 45 | swap(v, left, (left + right)/2); 46 | last = left; 47 | for(i = left+1; i <= right; i++) 48 | if((*comp)(v[i], v[left]) < 0) 49 | swap(v, ++last, i); 50 | swap(v, left, last); 51 | new_qsort(v, left, last-1, comp); 52 | new_qsort(v, last+1, right, comp); 53 | } 54 | 55 | void swap(void *v[], int i, int j) 56 | { 57 | void *temp; 58 | temp = v[i]; 59 | v[i] = v[j]; 60 | v[j] = temp; 61 | } 62 | 63 | /* 64 | 65 | Compare two strings numerically. 66 | 67 | */ 68 | int numcmp(const char *s1, const char *s2) 69 | { 70 | double v1, v2; 71 | v1 = atof(s1); 72 | v2 = atof(s2); 73 | if(v1 < v2) 74 | return -1; 75 | else if (v1 > v2) 76 | return 1; 77 | else 78 | return 0; 79 | } 80 | 81 | /* 82 | 83 | read input lines 84 | 85 | Use getline to obtain data from stdin. 86 | 87 | Put data into array of pointers to char, as long as number of lines less than maxlines. 88 | 89 | */ 90 | int readlines(char *lineprt[], int maxlines) 91 | { 92 | int len, nlines; 93 | char *p, line[MAXLEN]; 94 | 95 | nlines = 0; 96 | while((len = getline2(line, MAXLEN)) > 0) 97 | { 98 | if(nlines >= maxlines || (p = alloc(len)) == NULL) 99 | { 100 | return -1; 101 | } else { 102 | line[len-1] = '\0'; // delete newline 103 | strcpy(p, line); 104 | lineptr[nlines++] = p; 105 | } 106 | } 107 | return nlines; 108 | } 109 | 110 | /* 111 | write ouput lines to stdout 112 | 113 | input is an array of pointers to char and the number of lines stored 114 | 115 | ouput goes to stdout 116 | 117 | */ 118 | void writelines(char *lineptr[], int nlines) 119 | { 120 | while(nlines-- > 0) 121 | printf("%s\n", *lineptr++); 122 | } 123 | 124 | char *alloc(int n) 125 | { 126 | if(allocbuf + ALLOCSIZE - allocp >= n) 127 | { 128 | allocp += n; 129 | return allocp -n; // old pointer 130 | } else { 131 | return 0; 132 | } 133 | } 134 | 135 | /* 136 | 137 | Sort lines in input numerically. 138 | 139 | If -n switch entered, sort lines numerically. 140 | 141 | Program is limited by buffers of both maximum number of lines and maximum length of lines. 142 | 143 | */ 144 | 145 | int main(int argc, char *argv[]) 146 | { 147 | int nlines; 148 | int numeric = 0; 149 | 150 | if(argc > 1 && strcmp(argv[1], "-n") == 0) 151 | numeric = 1; 152 | if((nlines = readlines(lineptr, MAXLINES)) >= 0) 153 | { 154 | new_qsort((void **) lineptr, 0, nlines-1, (int(*)(void *,void *))(numeric ? &numcmp : &strcmp)); 155 | writelines(lineptr, nlines); 156 | return 0; 157 | } else { 158 | printf("Input: Too many lines to sort\n"); 159 | return 1; 160 | } 161 | return 1; 162 | } 163 | -------------------------------------------------------------------------------- /ex_5-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* print contents of array */ 5 | void print_array(char s[]) 6 | { 7 | int i; 8 | for(i = 0; i < strlen(s); i++) 9 | printf("%c", s[i]); 10 | printf("\n"); 11 | } 12 | 13 | /* previous version of strcat */ 14 | void strcat_old(char s[], char t[]) 15 | { 16 | int i, j; 17 | i = j = 0; 18 | while(s[i] != '\0') 19 | i++; 20 | while((s[i++] = t[j++]) != '\0') 21 | ; 22 | } 23 | 24 | /* copy string of chars from t into s */ 25 | void strcopy(char *s, char *t) 26 | { 27 | while(((*s++) = (*t++))); 28 | } 29 | 30 | /* pointer version of strcat - add string t to end of string s */ 31 | void strcatptr(char *s, char *t) 32 | { 33 | while(*s) { ++s; } // find pointer val for end of string s 34 | strcopy(s,t); 35 | } 36 | 37 | int main() 38 | { 39 | char buffer[128]; 40 | char s[] = { "this is a string of chars " }; 41 | 42 | strcatptr(buffer,s); 43 | strcatptr(buffer,s); 44 | 45 | print_array(buffer); 46 | 47 | printf("\n"); 48 | 49 | return 1; 50 | } 51 | -------------------------------------------------------------------------------- /ex_5-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* strcmp - compare strings */ 4 | /* return <0 is s < t; 0 if s==t; >0 if s>t */ 5 | int strcmp(char *s, char *t) 6 | { 7 | int i; 8 | for(i = 0; s[i] == t[i]; i++) 9 | ; 10 | 11 | return i; // might need to change 12 | } 13 | 14 | 15 | /* strlen - return length of string */ 16 | int strlen_new(char *s) 17 | { 18 | int n; 19 | for(n =0; s[n] != '\0'; n++); 20 | return n; 21 | } 22 | 23 | /* strend - compares end of string s with string t */ 24 | /* return 1 if string t occurs and 0 otherwise */ 25 | int strend(char *s, char *t) 26 | { 27 | int ls, lt; 28 | ls = strlen_new(s); // find length of s 29 | lt = strlen_new(t); // find length of t 30 | 31 | // start with end of s and move back comparing with end of t 32 | while(lt >= 0) 33 | { 34 | if(s[ls] != t[lt]) { return 0; } 35 | --ls; 36 | --lt; 37 | } 38 | return 1; 39 | } 40 | 41 | 42 | int main() 43 | { 44 | char s[] = { "1234567890" }; 45 | char t[] = { "67890" }; 46 | 47 | int ans = strend(s,t); 48 | printf("%d\n", ans); 49 | 50 | return 1; 51 | } 52 | -------------------------------------------------------------------------------- /ex_5-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* print contents of array */ 5 | void print_array(char s[]) 6 | { 7 | int i; 8 | for(i = 0; i < strlen(s); i++) 9 | printf("%c", s[i]); 10 | printf("\n"); 11 | } 12 | 13 | /* 14 | 15 | Copy at most n chars of string source into string dest. 16 | Pad with '\0' if source has fewer chars than dest. 17 | 18 | */ 19 | char *ss_strncpy(char *dest, const char *source, int n) 20 | { 21 | char *d = dest; 22 | if(n >= 0 || n == '\0') 23 | { 24 | while(--n >= 0 && (*dest++ = *source++) != '\0') 25 | continue; 26 | while(--n > 0) 27 | *dest++ = '\0'; 28 | } 29 | 30 | return d; 31 | } 32 | 33 | /* 34 | 35 | Appends n chars of src into the end of dest. 36 | 37 | If null encountered before n, then copy null and no more. 38 | 39 | If n is zero or negative, then function has not effect. 40 | 41 | */ 42 | char *ss_strncat(char *dest, const char *source, size_t n) 43 | { 44 | while(*dest) { ++dest; } // find pointer val for end of string s 45 | while((--n >= 0) && (*dest++ = *source++) != '\0'); 46 | return dest; 47 | } 48 | 49 | /* 50 | 51 | Compares contents of string s1 with contents of s2. 52 | 53 | If s1 < s2 returns < 0 54 | If s1 == s2 returns 0 55 | If s1 > s2 returns > 0 56 | 57 | */ 58 | int ss_strncmp(const char *s1, const char *s2, size_t n) 59 | { 60 | 61 | while(--n >= 0 && *s1 != '\0' && *s2 != '\0') 62 | { 63 | 64 | if(*s1++ == *s2++) { continue; } 65 | if(*s1 > *s2) 66 | return 1; 67 | else 68 | return -1; 69 | } 70 | return 0; 71 | } 72 | 73 | int main() 74 | { 75 | char source[] = { "Destination after..." }; 76 | char dest[] = { "This is the destination before..." }; 77 | 78 | print_array(dest); 79 | 80 | char *ans; 81 | 82 | ans = strncpy(dest, source, 20); 83 | 84 | printf("%p\n", ans); 85 | print_array(dest); 86 | 87 | printf("\n\n"); 88 | 89 | char source2[] = { "Destination after..." }; 90 | char dest2[] = { "This is the destination before..." }; 91 | 92 | print_array(dest2); 93 | 94 | ss_strncpy(dest2, source2, 20); 95 | 96 | printf("%p\n", ans); 97 | print_array(dest2); 98 | 99 | char info[] = { "Data To Append. " }; 100 | char buffer[50] = { "Beginning of Buffer. " }; 101 | int i; 102 | for(i = 0; i <= 2; ++i) 103 | { 104 | strncat(buffer, info, strlen(info)); 105 | print_array(buffer); 106 | } 107 | 108 | printf("\n"); 109 | 110 | char smaller[] = { "12345" }; 111 | char bigger[] = { "67890" }; 112 | int size_ans; 113 | 114 | size_ans = ss_strncmp(smaller, bigger, 3); 115 | printf("%d\n", size_ans); 116 | 117 | size_ans = ss_strncmp(bigger, bigger, 3); 118 | printf("%d\n", size_ans); 119 | 120 | size_ans = ss_strncmp(bigger, smaller, 3); 121 | printf("%d\n", size_ans); 122 | 123 | return 1; 124 | } 125 | -------------------------------------------------------------------------------- /ex_5-7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAXLINES 1000 // max number of lines in buffer 5 | #define MAXLEN 1000 // max length of individual lines 6 | 7 | // declare lines as an array of array of char 8 | // iow: create the maximum size empty buffer in global scope 9 | char lines[MAXLINES][MAXLEN]; 10 | 11 | /* print contents of array */ 12 | void print_sparse_array(char s[][MAXLEN]) 13 | { 14 | int i, j; 15 | for(i = 0; i < MAXLINES; i++) 16 | { 17 | int found = 0; 18 | for(j = 0; j < MAXLEN; j++) 19 | { 20 | if(s[i][j] != '\0') { found++; } 21 | } 22 | if(found > 0) 23 | { 24 | printf("%d:\t", i); 25 | for(j = 0; j < MAXLEN; j++) 26 | printf("%c", s[i][j]); 27 | printf("\n"); 28 | } 29 | } 30 | printf("\n"); 31 | } 32 | 33 | /* From K&R Page 29 */ 34 | int getline2(char s[], int lim) 35 | { 36 | int c, i; 37 | for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++) 38 | s[i] = c; 39 | if (c == '\n') 40 | s[i++] = c; 41 | s[i] = '\0'; 42 | return i; 43 | } 44 | 45 | 46 | /* 47 | 48 | reads input lines from stdin and stores each line in buffer named lines 49 | 50 | */ 51 | int readlines(char lines[][MAXLEN], int maxlines) 52 | { 53 | int len, nlines = 0; 54 | 55 | while((len = getline2(lines[nlines], MAXLEN)) > 0) 56 | if(nlines >= maxlines) // when buffer full, return 57 | return -1; 58 | else 59 | lines[nlines++][len - 1] = '\0'; // delete newline 60 | return nlines; 61 | } 62 | 63 | int main() 64 | { 65 | int length = readlines(lines, MAXLINES); 66 | printf("Number of Lines Stored: %d\n", length); 67 | print_sparse_array(lines); 68 | return 1; 69 | } 70 | -------------------------------------------------------------------------------- /ex_5-8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static char daytab[2][13] = 4 | { 5 | { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 6 | { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } 7 | }; 8 | 9 | /* set day of year based on month and day */ 10 | int day_of_year(int year, int month, int day) 11 | { 12 | int i, leap; 13 | 14 | if(year < 1752 || month > 12 || month < 1 || day < 1) { return -1; } 15 | 16 | leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); 17 | if(day > daytab[leap][month]) { return -1; } // day month pair OK? 18 | 19 | for( i = 1; i < month; i++) 20 | day += daytab[leap][i]; 21 | return day; 22 | } 23 | 24 | /* get month, day from day of year */ 25 | int month_day(int year, int yearday, int *pmonth, int *pday) 26 | { 27 | int i, leap; 28 | 29 | if(year < 1752) { return -1; } 30 | 31 | leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); 32 | if((leap && yearday > 366) || (!leap && yearday > 365)) { return -1; } 33 | 34 | for(i = 1; yearday > daytab[leap][i]; i++) 35 | yearday -= daytab[leap][i]; 36 | *pmonth = i; 37 | *pday = yearday; 38 | return 1; 39 | } 40 | 41 | int main() 42 | { 43 | int ans, month, day; 44 | ans = day_of_year(2009, 2, 1); 45 | printf("%d\n", ans); 46 | ans = month_day(2009, 34, &month, &day); 47 | 48 | if(ans) 49 | printf("Day: %d\tMonth:%d\n", day, month); 50 | 51 | int yearday, year = 2009; 52 | int leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); 53 | 54 | for(month = 1; month <= 12; ++month) 55 | { 56 | printf("\n"); 57 | day = 0; 58 | while(++day <= daytab[leap][month]) 59 | { 60 | yearday = day_of_year(year, month, day); 61 | printf("Y: %d\tM: %d\tD: %d\t->\tYD:%d\n", year, month, day, yearday); 62 | } 63 | printf("\n"); 64 | } 65 | return 1; 66 | } 67 | -------------------------------------------------------------------------------- /for.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | double nc; 6 | 7 | for (nc = 0; getchar() != EOF; ++nc) 8 | ; 9 | printf("%.0f\n", nc); 10 | } 11 | -------------------------------------------------------------------------------- /get_chars.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int c; 6 | c = getchar(); 7 | while ((c = getchar()) != EOF){ 8 | putchar(c); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("hello world\n"); 6 | } 7 | -------------------------------------------------------------------------------- /limits_of_c_lang.c: -------------------------------------------------------------------------------- 1 | /* 2 | Lists limits of C Programming Language Types. 3 | 4 | NOTE: Must compile with -std=c99 in gcc. 5 | 6 | Steven Schronk 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | printf("\nThis Program Displays The Limits of Datatypes on this System.\n\n"); 16 | printf("Bits in a Single Char: %d\n", CHAR_BIT); 17 | printf("Max Bytes in Multibyte Char (Any Locale): %d\n", MB_LEN_MAX); 18 | printf("\n"); 19 | printf("Char Min:\t\t%+d\n", CHAR_MIN); 20 | printf("Char Max:\t\t%+d\n", CHAR_MAX); 21 | printf("Signed Char Min:\t%+d\n", SCHAR_MIN); 22 | printf("Signed Char Max:\t%+d\n", SCHAR_MAX); 23 | printf("Unsigned Char Max:\t%+d\n", UCHAR_MAX); 24 | printf("\n"); 25 | printf("Short Min:\t\t%+d\n", SHRT_MIN); 26 | printf("Short Max:\t\t%+d\n", SHRT_MAX); 27 | printf("Unsigned Short Max:\t%+d\n", USHRT_MAX); 28 | printf("\n"); 29 | printf("Integer Min:\t\t%+d\n", INT_MIN); 30 | printf("Integer Max:\t\t%+d\n", INT_MAX); 31 | printf("Unsigned Integer Max:\t%+d\n", UINT_MAX); 32 | printf("\n"); 33 | printf("Long Min:\t\t%+ld\n", LONG_MIN); 34 | printf("Long Max:\t\t%+ld\n", LONG_MAX); 35 | printf("Unsigned Long Max:\t+%lu\n", ULONG_MAX); 36 | printf("\n"); 37 | printf("Long Long Min:\t\t%+lld\n", LLONG_MIN); 38 | printf("Long Long Max:\t\t%+lld\n", LLONG_MAX); 39 | printf("Unsigned Long Long Max: +%llu\n", ULLONG_MAX); 40 | printf("\n"); 41 | return EXIT_SUCCESS; 42 | } 43 | -------------------------------------------------------------------------------- /line_counter.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int c, nl; 6 | nl = 0; 7 | while((c = getchar()) != EOF) 8 | if( c == '\n') 9 | ++nl; 10 | printf("%d\n", nl); 11 | } 12 | -------------------------------------------------------------------------------- /primes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int isPrime(int); 5 | 6 | int main(void) 7 | { 8 | int num = 2; 9 | int max; 10 | 11 | printf("Input Maximum Prime: "); 12 | scanf("%d", &max); 13 | 14 | while ( num < max ) 15 | { 16 | if(isPrime(num)) { printf("%d\n", num); } 17 | num++; 18 | } 19 | return 0; 20 | } 21 | 22 | int isPrime(int n) 23 | { 24 | int dev; 25 | 26 | for(dev = 2; dev <= n/2; dev++) 27 | { 28 | if((n % dev) == 0) { return false; } 29 | } 30 | return true; 31 | } 32 | -------------------------------------------------------------------------------- /ranges.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Prints ranges of data types in C 4 | 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | printf("\n\nThis Computer Supports the following\n"); 13 | printf("Datatype specifications:\n\n"); 14 | 15 | int i = sizeof(int); 16 | printf("Size of Int: %d\n", i); 17 | printf("INT_MIN: %d\n", INT_MIN); 18 | printf("INT_MAX: %d\n", INT_MAX); 19 | printf("\n"); 20 | 21 | i = sizeof(char); 22 | printf("Size of Char: %d\n", i); 23 | printf("CHAR_BIT: %d\n", CHAR_BIT); 24 | printf("CHAR_MAX: %d\n", CHAR_MAX); 25 | printf("CHAR_MIN: %d\n", CHAR_MIN); 26 | printf("\n"); 27 | 28 | i = sizeof(double); 29 | printf("Size of Double: %d\n", i); 30 | printf("\n"); 31 | 32 | i = sizeof(long); 33 | printf("Size of Long: %d\n", i); 34 | printf("LONG_MIN: %ld\n", LONG_MIN); 35 | printf("LONG_MAX: %ld\n", LONG_MAX); 36 | printf("\n"); 37 | 38 | i = sizeof(short); 39 | printf("Size of Short: %d\n", i); 40 | printf("SHRT_MIN: %d\n", SHRT_MIN); 41 | printf("SHRT_MAX: %d\n", SHRT_MAX); 42 | printf("\n"); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /secure.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | printf("%s", "Welcome!"); 5 | } 6 | -------------------------------------------------------------------------------- /shell_sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Invented in 1959 by D. L. Shell 4 | 5 | Sorts an array of integers 6 | 7 | From Page 60 & 61. 8 | 9 | */ 10 | 11 | #include 12 | 13 | void shellsort(int v[], int n) 14 | { 15 | int gap, i, j, temp; 16 | 17 | for(gap = n/2; gap > 0; gap /=2) 18 | { 19 | for(i = gap; i < n; i++) 20 | { 21 | for(j = i - gap; j >=0 && v[j]>v[j+gap]; j-=gap) 22 | { 23 | temp = v[j]; 24 | v[j] = v[j+gap]; 25 | v[j+gap] = temp; 26 | } 27 | } 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | int data[10] = { 9, 12, 54, 90, 0, 100, 65, 32, 54, 81}; 34 | int i; 35 | 36 | for(i = 0; i < 10; ++i) { printf("%d\n", data[i]); } 37 | 38 | shellsort(data, 10); 39 | 40 | for(i = 0; i < 10; ++i) { printf("%d\n", data[i]); } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /srs_lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "srs_lib.h" 5 | 6 | /* print contents of array */ 7 | void print_array(char s[]) 8 | { 9 | int i; 10 | for(i = 0; i < strlen(s); i++) 11 | printf("%c", s[i]); 12 | printf("\n"); 13 | } 14 | 15 | /* print contents of array */ 16 | void print_sparse_array(char s[][MAXLEN]) 17 | { 18 | int i, j; 19 | for(i = 0; i < MAXLINES; i++) 20 | { 21 | int found = 0; 22 | for(j = 0; j < MAXLEN; j++) 23 | { 24 | if(s[i][j] != '\0') { found++; } 25 | } 26 | if(found > 0) 27 | { 28 | printf("%d:\t", i); 29 | for(j = 0; j < MAXLEN; j++) 30 | printf("%c", s[i][j]); 31 | printf("\n"); 32 | } 33 | } 34 | printf("\n"); 35 | } 36 | 37 | /* reverse contents of array in place */ 38 | void reverse(char s[]) 39 | { 40 | int c, i, j; 41 | for(i = 0, j = strlen(s)-1; i < j; i++, j--) 42 | { 43 | c = s[i]; 44 | s[i] = s[j]; 45 | s[j] = c; 46 | } 47 | } 48 | 49 | /* convert integer to another base and store result in array of char */ 50 | int itob(int n, char s[], int b) 51 | { 52 | int sign, i = 0; 53 | 54 | //create string of digits used to represent chars 55 | char base_chars[] = { "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" }; 56 | 57 | //check that base is neither too high nor too small 58 | if(b < 2) 59 | { 60 | printf("Base must be between 2 and %d.\n", (int)strlen(base_chars)-1); 61 | return -1; 62 | } 63 | 64 | if(b > strlen(base_chars)-1) 65 | { 66 | printf("Base must be %d or less.\n", (int)strlen(base_chars)-1); 67 | return -1; 68 | } 69 | 70 | // remove sign from number 71 | if(n < 0) { n = -n; sign = 1; } 72 | 73 | 74 | // increment s array and store in that location the modulus of the number -vs- the base 75 | // while number divided by base is larger than 0 76 | i = 0; 77 | do { 78 | s[i++] = base_chars[n % b]; 79 | } while ((n /= b) > 0); 80 | 81 | // add sign from above 82 | if(sign == '1') { s[++i] = '-'; } 83 | s[i] = '\0'; 84 | 85 | reverse(s); 86 | return 1; 87 | } 88 | 89 | // This dummy call added to get build makefile to work 90 | int main() {} 91 | -------------------------------------------------------------------------------- /srs_lib.h: -------------------------------------------------------------------------------- 1 | #define MAXLINES 1000 2 | #define MAXLEN 1000 3 | 4 | void print_array(char s[]); 5 | void print_sparse_array(char s[][MAXLEN]); 6 | int itob(int n, char s[], int b); 7 | 8 | -------------------------------------------------------------------------------- /stack_address.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program illustrates stack randomization of operating systems. 3 | 4 | Operating systems attempt to prevent security vulnerability by 5 | randomizing the stack location when a program starts. 6 | 7 | This helps prevent buffer overflow attacks. 8 | 9 | To test and see if this security measure is built into your 10 | system, run this program several times; if the stack address 11 | changes each time, it is likely that this is implemented on 12 | your system. 13 | */ 14 | 15 | #include 16 | 17 | int main() 18 | { 19 | int local; 20 | printf("local at %p\n", &local); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct symbol_hash { 4 | char *name; 5 | int address; 6 | }; 7 | 8 | int main() 9 | { 10 | struct symbol_hash symbol_hash[512]; 11 | 12 | symbol_hash[0].name = "This is a long string of data"; 13 | 14 | printf("hash name %s\n", symbol_hash[0].name); 15 | 16 | symbol_hash[0].name = "This is a short string."; 17 | 18 | printf("hash name %s\n", symbol_hash[0].name); 19 | 20 | symbol_hash[0].address = 4; 21 | 22 | printf("hash add %d\n", symbol_hash[0].address); 23 | return 1; 24 | } 25 | -------------------------------------------------------------------------------- /temp_conv.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int fahr, celsius; 6 | int lower, upper, step; 7 | 8 | lower = 0; 9 | upper = 300; 10 | step = 20; 11 | 12 | fahr = lower; 13 | while (fahr <= upper){ 14 | celsius = 5 * (fahr-32) / 9; 15 | printf("%d\t%d\n", fahr, celsius); 16 | fahr = fahr + step; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /temp_conv2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float fahr, celsius; 6 | int lower, upper, step; 7 | 8 | lower = 0; 9 | upper = 300; 10 | step = 20; 11 | 12 | fahr = lower; 13 | while (fahr <= upper){ 14 | celsius = (5.0/9.0) * (fahr-32.0); 15 | printf("%3.0f %6.1f\n", fahr, celsius); 16 | fahr = fahr + step; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test_get_char.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int test; 6 | 7 | test = getchar() != EOF; 8 | 9 | printf("%d\n", test); 10 | } 11 | -------------------------------------------------------------------------------- /variable_arrays.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Demonstation of variable arrays. 4 | Only works with compilers that support C99 5 | 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int i, n; 13 | 14 | printf("How many numbers do you want to reverse? "); 15 | scanf("%d", &n); 16 | 17 | int a[n]; /* C99 Only */ 18 | printf("Enter %d numbers: ", n); 19 | for(i = 0; i < n; i++) 20 | scanf("%d", &a[i]); 21 | 22 | printf("In reverse order:"); 23 | for(i = n - 1; i >= 0; i--) 24 | printf(" %d", a[i]); 25 | printf("\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /word_count.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define IN 1 4 | #define OUT 0 5 | 6 | int main() 7 | { 8 | int c, nl, nw, nc, state; 9 | 10 | state = OUT; 11 | 12 | nl = nw = nc = 0; 13 | while ((c = getchar()) != EOF) 14 | { 15 | ++nc; 16 | if(c == '\n') 17 | ++nl; 18 | if(c == ' ' || c == '\n' || c == '\t') 19 | state = OUT; 20 | else if(state == OUT) 21 | { 22 | state = IN; 23 | ++nw; 24 | } 25 | } 26 | printf("%d %d %d\n", nl, nw, nc); 27 | } 28 | --------------------------------------------------------------------------------