Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Through 2.3

  • Loading branch information...
commit 0dacec30f8d4eb581b5014d2cf869bf401760fab 1 parent 29c4337
Jimmy Li jyli7 authored
66 1.21_entab.c
... ... @@ -0,0 +1,66 @@
  1 +#include <stdio.h>
  2 +
  3 +/* Pseudocode
  4 +1) Count the number of blanks
  5 +2) Replace the appropriate # of blanks with tabs, e.g. 2 blanks = tab.
  6 +3) Print out the character
  7 +*/
  8 +
  9 +#define BLANKS 2
  10 +#define MAXLINE 1000
  11 +
  12 +int getline2(char line[], int maxline);
  13 +void tab_replace(char line[], int len);
  14 +void clear_line(char line[], int len);
  15 +
  16 +main() {
  17 + int len;
  18 + char line[MAXLINE];
  19 + char new_line[MAXLINE];
  20 + while ((len = getline2(line, MAXLINE)) > 0)
  21 + {
  22 + tab_replace(line, len);
  23 + printf("%s\n", line);
  24 + clear_line(line, len);
  25 + }
  26 +}
  27 +
  28 +int getline2(char s[], int lim)
  29 +{
  30 + int c, i;
  31 +
  32 + for (i=0; i<lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
  33 + s[i] = c;
  34 + if (c == '\n') {
  35 + s[i] = c;
  36 + ++i;
  37 + }
  38 + s[i] = '\0';
  39 + return i;
  40 +}
  41 +
  42 +void tab_replace(char s[], int len)
  43 +{
  44 + int i, j, k;
  45 + for (i = 0; i < len - 1; ++i) {
  46 + if (s[i] == ' ' && s[i+1] == ' ' && s[i+2] == ' '){
  47 + s[i] = '\t';
  48 + for (j = i; j < len - 3; ++j)
  49 + {
  50 + s[j+1] = s[j+3];
  51 + s[j+2] = s[j+4];
  52 + }
  53 + s[len-1] = ' ';
  54 + s[len-2] = ' ';
  55 + --len;
  56 + }
  57 + }
  58 +}
  59 +
  60 +void clear_line(char s[], int len)
  61 +{
  62 + int i;
  63 + for (i = 0; i < len; ++i) {
  64 + s[i] = ' ';
  65 + }
  66 +}
24 2.1_type_ranges.c
... ... @@ -0,0 +1,24 @@
  1 +#include <stdio.h>
  2 +#include <limits.h>
  3 +
  4 +/* From http://users.powernet.co.uk/eton/kandr2/krx201.html */
  5 +
  6 +int
  7 +main ()
  8 +{
  9 + printf("Size of Char %d\n", CHAR_BIT);
  10 + printf("Size of Char Max %d\n", CHAR_MAX);
  11 + printf("Size of Char Min %d\n", CHAR_MIN);
  12 + printf("Size of int min %d\n", INT_MIN);
  13 + printf("Size of int max %d\n", INT_MAX);
  14 + printf("Size of long min %ld\n", LONG_MIN); /* RB */
  15 + printf("Size of long max %ld\n", LONG_MAX); /* RB */
  16 + printf("Size of short min %d\n", SHRT_MIN);
  17 + printf("Size of short max %d\n", SHRT_MAX);
  18 + printf("Size of unsigned char %u\n", UCHAR_MAX); /* SF */
  19 + printf("Size of unsigned long %lu\n", ULONG_MAX); /* RB */
  20 + printf("Size of unsigned int %u\n", UINT_MAX); /* RB */
  21 + printf("Size of unsigned short %u\n", USHRT_MAX); /* SF */
  22 +
  23 + return 0;
  24 +}
23 2.2_alternative_for.c
... ... @@ -0,0 +1,23 @@
  1 +#include <stdio.h>
  2 +
  3 +#define MAX_STRING_LENGTH 100
  4 +
  5 +int main(void)
  6 +{
  7 + int i, c, lim = MAX_STRING_LENGTH;
  8 + char s[MAX_STRING_LENGTH];
  9 +
  10 + for (i=0; i<lim-1; ++i){
  11 + if ((c=getchar()) == '\n')
  12 + break;
  13 + else if (c == EOF)
  14 + break;
  15 + else
  16 + s[i] = c;
  17 + }
  18 +
  19 + s[i] = '\0';
  20 + return 0;
  21 +
  22 +}
  23 +
41 2.3_htoi.c
... ... @@ -0,0 +1,41 @@
  1 +#include <stdio.h>
  2 +
  3 +int atoi(char s[]);
  4 +
  5 +int main(void)
  6 +{
  7 + int result = htoi("1f");
  8 + printf("%d", result);
  9 +
  10 + result = htoi("0X1f");
  11 + printf("%d", result);
  12 +}
  13 +
  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 +int htoi(char s[])
  25 +{
  26 + int i, n;
  27 +
  28 + n = 0;
  29 + i = 0;
  30 + if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  31 + i = 2;
  32 + for (i; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f') || (s[i] >= 'A' && s[i] <= 'F'); ++i){
  33 + if (s[i] >= '0' && s[i] <= '9')
  34 + n = 16*n + (s[i]-'0');
  35 + else if (s[i] >= 'A' && s[i] <= 'F')
  36 + n = 16*n + (s[i]-'A' + 10);
  37 + else
  38 + n = 16*n + (s[i]-'a' + 10);
  39 + }
  40 + return n;
  41 +}

0 comments on commit 0dacec3

Please sign in to comment.
Something went wrong with that request. Please try again.