Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Through 2.3

  • Loading branch information...
commit 0dacec30f8d4eb581b5014d2cf869bf401760fab 1 parent 29c4337
@jyli7 jyli7 authored
View
66 1.21_entab.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+
+/* Pseudocode
+1) Count the number of blanks
+2) Replace the appropriate # of blanks with tabs, e.g. 2 blanks = tab.
+3) Print out the character
+*/
+
+#define BLANKS 2
+#define MAXLINE 1000
+
+int getline2(char line[], int maxline);
+void tab_replace(char line[], int len);
+void clear_line(char line[], int len);
+
+main() {
+ int len;
+ char line[MAXLINE];
+ char new_line[MAXLINE];
+ while ((len = getline2(line, MAXLINE)) > 0)
+ {
+ tab_replace(line, len);
+ printf("%s\n", line);
+ clear_line(line, len);
+ }
+}
+
+int getline2(char s[], int lim)
+{
+ int c, i;
+
+ for (i=0; i<lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
+ s[i] = c;
+ if (c == '\n') {
+ s[i] = c;
+ ++i;
+ }
+ s[i] = '\0';
+ return i;
+}
+
+void tab_replace(char s[], int len)
+{
+ int i, j, k;
+ for (i = 0; i < len - 1; ++i) {
+ if (s[i] == ' ' && s[i+1] == ' ' && s[i+2] == ' '){
+ s[i] = '\t';
+ for (j = i; j < len - 3; ++j)
+ {
+ s[j+1] = s[j+3];
+ s[j+2] = s[j+4];
+ }
+ s[len-1] = ' ';
+ s[len-2] = ' ';
+ --len;
+ }
+ }
+}
+
+void clear_line(char s[], int len)
+{
+ int i;
+ for (i = 0; i < len; ++i) {
+ s[i] = ' ';
+ }
+}
View
24 2.1_type_ranges.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <limits.h>
+
+/* From http://users.powernet.co.uk/eton/kandr2/krx201.html */
+
+int
+main ()
+{
+ printf("Size of Char %d\n", CHAR_BIT);
+ printf("Size of Char Max %d\n", CHAR_MAX);
+ printf("Size of Char Min %d\n", CHAR_MIN);
+ printf("Size of int min %d\n", INT_MIN);
+ printf("Size of int max %d\n", INT_MAX);
+ printf("Size of long min %ld\n", LONG_MIN); /* RB */
+ printf("Size of long max %ld\n", LONG_MAX); /* RB */
+ printf("Size of short min %d\n", SHRT_MIN);
+ printf("Size of short max %d\n", SHRT_MAX);
+ printf("Size of unsigned char %u\n", UCHAR_MAX); /* SF */
+ printf("Size of unsigned long %lu\n", ULONG_MAX); /* RB */
+ printf("Size of unsigned int %u\n", UINT_MAX); /* RB */
+ printf("Size of unsigned short %u\n", USHRT_MAX); /* SF */
+
+ return 0;
+}
View
23 2.2_alternative_for.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+#define MAX_STRING_LENGTH 100
+
+int main(void)
+{
+ int i, c, lim = MAX_STRING_LENGTH;
+ char s[MAX_STRING_LENGTH];
+
+ for (i=0; i<lim-1; ++i){
+ if ((c=getchar()) == '\n')
+ break;
+ else if (c == EOF)
+ break;
+ else
+ s[i] = c;
+ }
+
+ s[i] = '\0';
+ return 0;
+
+}
+
View
41 2.3_htoi.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+
+int atoi(char s[]);
+
+int main(void)
+{
+ int result = htoi("1f");
+ printf("%d", result);
+
+ result = htoi("0X1f");
+ printf("%d", result);
+}
+
+int atoi(char s[])
+{
+ int i, n;
+
+ n = 0;
+ for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
+ n = 10*n + (s[i]-'0');
+ return n;
+}
+
+int htoi(char s[])
+{
+ int i, n;
+
+ n = 0;
+ i = 0;
+ if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
+ i = 2;
+ for (i; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f') || (s[i] >= 'A' && s[i] <= 'F'); ++i){
+ if (s[i] >= '0' && s[i] <= '9')
+ n = 16*n + (s[i]-'0');
+ else if (s[i] >= 'A' && s[i] <= 'F')
+ n = 16*n + (s[i]-'A' + 10);
+ else
+ n = 16*n + (s[i]-'a' + 10);
+ }
+ return n;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.