diff options
author | sinanmohd <pcmsinan@gmail.com> | 2022-06-04 12:11:15 +0530 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2022-06-04 12:11:15 +0530 |
commit | c24973af02bc33f2f5f25d37e22ca91da5de3c47 (patch) | |
tree | 460a005b6a3a9a967bd881bc2bf01e0becbe33cc |
inital commit
-rw-r--r-- | 1.13.c | 50 | ||||
-rw-r--r-- | 1.14.c | 50 | ||||
-rw-r--r-- | 1.15.c | 24 | ||||
-rw-r--r-- | 1.16.c | 52 | ||||
-rw-r--r-- | 1.17.c | 36 | ||||
-rw-r--r-- | 1.18.c | 35 | ||||
-rw-r--r-- | 1.19.c | 70 | ||||
-rw-r--r-- | 1.20.c | 32 | ||||
-rw-r--r-- | 1.21.c | 33 | ||||
-rw-r--r-- | 1.22.c | 31 | ||||
-rw-r--r-- | 1.23.c | 56 | ||||
-rw-r--r-- | 1.24.c | 161 | ||||
-rw-r--r-- | 2.1.c | 55 | ||||
-rw-r--r-- | 2.10.c | 20 | ||||
-rw-r--r-- | 2.2.c | 65 | ||||
-rw-r--r-- | 2.3.c | 34 | ||||
-rw-r--r-- | 2.4.c | 35 | ||||
-rw-r--r-- | 2.5.c | 32 | ||||
-rw-r--r-- | 2.6.c | 47 | ||||
-rw-r--r-- | 2.7.c | 44 | ||||
-rw-r--r-- | 2.8.c | 55 | ||||
-rw-r--r-- | 2.9.c | 23 | ||||
-rw-r--r-- | 3.1.c | 56 | ||||
-rw-r--r-- | 3.2.c | 119 | ||||
-rw-r--r-- | 3.4.c | 57 |
25 files changed, 1272 insertions, 0 deletions
@@ -0,0 +1,50 @@ +#include <stdio.h> + +#define MAX_LEN 10 + +// Programm to produce histogram based on the length of words + +int +main() +{ + int i, j, input, word_len, big_words; + int len[MAX_LEN]; + + word_len = big_words = 0; + for (i = 0; i< MAX_LEN; i++) + len[i]=0; + + while ((input = getchar()) != EOF) { + if(input == ' ' || input == '\n' || input == '\t') { + if (word_len >= MAX_LEN) + big_words++; + else + len[word_len]++; + word_len = 0; + } + else + word_len++; + } + + if(input != ' ' && input != '\n' && input != '\t') { + if (word_len >= MAX_LEN) + big_words++; + else + len[word_len]++; + } + + printf("\n"); + + for (i = 0; i< MAX_LEN; i++) { + printf(" %d -- %3d: ", i, len[i]); + for (j =0; j < len[i]; j++) + printf("*"); + printf("\n"); + } + + printf(">%d -- %3d: ", (MAX_LEN-1), big_words); + for (j =0; j < big_words; j++) + printf("*"); + printf("\n"); +} + @@ -0,0 +1,50 @@ +#include <stdio.h> + +#define MAX_CHAR 128 // ASCII set + +int t0; + +/* program to print a histogram of the frequencies of diffrent + * characters in its input */ + +int t1; + +// this is a test1 + +int t2; + +/* trrrrrr2 */ + +int +main(void) +{ + int i, j, input; + int char_count[MAX_CHAR]; + + for (i = 0; i < MAX_CHAR; i++) + char_count[i] = 0; + + while((input = getchar()) != EOF) { + char_count[input]++; + } + + printf("\n"); + + for (i =0; i < MAX_CHAR; i++) { + if (char_count[i] == 0) + continue; + + if (i == '\t') + printf(" \\t -- %3d: ", char_count[i]); + else if (i == '\n') + printf(" \\n -- %3d: ", char_count[i]); + else if (i == ' ') + printf("' ' -- %3d: ", char_count[i]); + else + printf(" %c -- %3d: ", i, char_count[i]); + + for (j = 0; j <= char_count[i]; j++) + printf("*"); + printf("\n"); + } +} @@ -0,0 +1,24 @@ +#include <stdio.h> + +#define TRUE 1 + +float conv_temp(int temp_c); + +int +main(void) +{ + int input, temp_c; + while(TRUE) { + printf("Enter temp in C: "); + scanf("%d", &temp_c); + printf("%d C is %.1f F\n", temp_c, conv_temp(temp_c)); + } + + return 0; +} + +float +conv_temp(int temp_c) +{ + return (9.0/5.0 * temp_c) + 32.0; +} @@ -0,0 +1,52 @@ +#include <stdio.h> + +#define MAXLINE 1000 + +int gline(char str[], int len); +void cp_str(char to[], char from[]); + +int +main(void) +{ + int len, max; + char str[MAXLINE]; + char mstr[MAXLINE]; + + max = 0; + + while ((len = gline(str, MAXLINE)) > 0) + if (len > max) { + cp_str(mstr, str); + max = len; + } + + if (max > 0) + printf("\nlength: %d\n%s", max, mstr); + + return 0; +} + +int +gline(char str[], int len) +{ + int i; + char input; + for (i = 0; i < len-1 && (input = getchar()) != EOF && input != '\n'; i++) + str[i] = input; + + if (input == '\n' && i < len-1) { + str[i] = '\n'; + ++i; + } + + str[i] = '\0'; + + return i; +} + +void +cp_str(char to[], char from[]) +{ + for(int i =0; (to[i] = from[i]) != '\0'; i++) + ; +} @@ -0,0 +1,36 @@ +#include <stdio.h> + +#define MAXLINE 1000 + +int gline(char str[], int len); + +int +main(void) +{ + int len; + char str[MAXLINE]; + + while ((len = gline(str, MAXLINE)) > 0) + if (len > 80) + printf("\nlength: %d\n%s\n", len, str); + + return 0; +} + +int +gline(char str[], int len) +{ + int i; + char input; + for (i = 0; i < len-1 && (input = getchar()) != EOF && input != '\n'; i++) + str[i] = input; + + if (input == '\n' && i < len-1) { + str[i] = '\n'; + ++i; + } + + str[i] = '\0'; + + return i; +} @@ -0,0 +1,35 @@ +#include <stdio.h> + +#define MAXLINE 1000 + +int +main(void) +{ + int input, i, non_blank; + char str[MAXLINE]; + + i = 0; + non_blank = -1; + + while ((input = getchar()) != EOF) { + while (input != '\n' && input != EOF) { + str[i] = input; + + if (input != ' ' && input != '\t') + non_blank = i; + + ++i; + input = getchar(); + } + + if (non_blank == -1) + continue; + + str[non_blank+1] = '\0'; + printf("%s\n", str); + + i = 0; + } + + return 0; +} @@ -0,0 +1,70 @@ +#include <stdio.h> + +#define MAXLINE 1000 + +void reverse(char str[]); +void str_cp(char to[], char from[]); + +int +main(void) +{ + int input, i; + char str[MAXLINE]; + + i = 0; + + while ((input = getchar()) != EOF) { + str[i] = input; + i++; + + if (input == '\n') { + str[i] = '\0'; + i = 0; + reverse(str); + printf("%s\n", str); + } + } + + return 0; +} + +void +str_cp(char to[], char from[]) +{ + int i, last_non_blank; + + last_non_blank = -1; + + for (i = 0; from[i] != '\0'; ++i) { + if (from[i] != ' '&& from[i] != '\n' && from[i] != '\t') + last_non_blank = i; + + to[i] = from[i]; + } + + if (last_non_blank >= 0) + to[last_non_blank+1] = '\0'; + else + to[i] = '\0'; +} + +void +reverse(char str[]) +{ + int i, len; + char storage[MAXLINE]; + + i = len = 0; + + str_cp (storage, str); + + for ( len = 0; storage[len] != '\0'; len++) + ; + + for ( i = 0; i < len; ++i) + str[i] = storage[len-1-i]; + + str[i] = '\0'; +} + + @@ -0,0 +1,32 @@ +#include <stdio.h> + +#define TAB 8 + +int +main(void) +{ + int input, count, space; + + count = 0; + + while ((input = getchar()) != EOF) { + if (input == '\t') { + space = (TAB - (count % TAB)); + + for (space = (TAB - (count % TAB)); space > 0; space--) { + printf(" "); + count++; + } + } + + else if (input == '\n') + count = 0; + + else { + count++; + printf("%c", input); + } + } + + return 0; +} @@ -0,0 +1,33 @@ +#include <stdio.h> + +#define TAB 8 + +int +main(void) +{ + int count, tab, blank; + char input; + + count = 0; + + while((input = getchar()) != EOF) { + + if (input == ' ') + count++; + + else { + + if (count > 0) { + for (tab = count/TAB; tab > 0; --tab) + printf("\t"); + for (blank = count%TAB; blank > 0; --blank) + printf(" "); + } + count = 0; + + printf("%c", input); + } + } + + return 0; +} @@ -0,0 +1,31 @@ +#include <stdio.h> + +#define LINE_LENGTH 230 +#define TAB 8 + +int +main(void) +{ + int count; + char input; + + count = 0; + + while ((input = getchar()) != EOF) { + if (input == '\n') + count = 0; + else if (input == '\t') + count = count + TAB; + else + count++; + + printf("%c", input); + + if (count >= LINE_LENGTH) { + printf("\n"); + count = 0; + } + } + + return 0; +} @@ -0,0 +1,56 @@ +#include <stdio.h> + +#define OUT 0 +#define STAR 1 +#define SLASH 2 + +/* this is a c programm to remove all comments from c programms */ +// including "slash comments" +/* and "star comments" */ +// without the use of arrays + +int +main(void) +{ + char input, temp; + int state; + + state = OUT; + + while ((input = getchar()) != EOF) { + + if (input == '/') { + temp = input; + + if ((input = getchar()) == '*') + state = STAR; + else if (input == '/') + state = SLASH; + else + printf("%c", temp); + + } + + else if (input == '*' && state == STAR) { + + if ((input = getchar()) == '/') { + state = OUT; + + if ((input = getchar()) != '\n') + printf("%c", input); + + continue; + } + } + + else if (input == '\n' && state == SLASH) { + state = OUT; + continue; + } + + if (state == OUT) + printf("%c", input); + } + + return 0; +} @@ -0,0 +1,161 @@ +#include <stdio.h> + +#define OUT 0 +#define FALSE -1 +#define TRUE 1 +#define STAR 1 +#define SLASH 2 + +void count(char input, int* parentheses, int* brackets, int* braces); + +/* this is a c programm to detect syntax errors in c programms */ + +int +main(void) +{ + char input; + int temp, state, line, parentheses, brackets, braces, single_quotes, double_quotes, double_quotes_line; + + state = OUT; + parentheses = brackets = braces = single_quotes = double_quotes = double_quotes_line = 0; + line = 1; + + while ((input = getchar()) != EOF) { + + if (input == '\n') { + line++; + continue; + } + + /* filiter out comments */ + + else if (input == '/') { + temp = input; + + if ((input = getchar()) == '*') { + state = STAR; + temp = FALSE; + } + + else if (input == '/') { + state = SLASH; + temp = FALSE; + } + + } + + else if (input == '*' && state == STAR) { + + if ((input = getchar()) == '/') { + state = OUT; + continue; + } + + } + + else if (input == '\n' && state == SLASH) { + state = OUT; + continue; + } + + if (temp != FALSE) + count(temp, &parentheses, &brackets, &braces); + + if (state == OUT) { + + if (input == '"' && double_quotes == TRUE) { + double_quotes = FALSE; + continue; + } + + /* process escape sequences */ + + else if (input == '\\') { + input = getchar(); + + if (input == '\n') { + line++; + continue; + } + + if (input != 'a' && input != 'b' && input != 'f' && input != 'n' && input != 'r' && + input != 't' && input != 'v' && input != '\\' && input != '\'' && input != '"' && input != '?' && input != '0') + printf("unproper escape sequence detected at line %d\n", line); + + else if (input == 'o') { + if((input = getchar()) != 'o' || (input = getchar()) != 'o') + printf("unproper escape sequence detected at line %d\n", line); + } + + else if (input == 'x') { + if((input = getchar()) != 'h' || (input = getchar()) != 'h') + printf("unproper escape sequence detected at line %d\n", line); + } + + else + continue; + } + + /* process double quotes */ + + else if (input == '"') { + double_quotes = TRUE; + double_quotes_line = line; + } + + /* process single quotes */ + + else if (input == '\'' && double_quotes != TRUE) { + ; + if ((input = getchar()) == '\\') { + input = getchar(); + } + + if ((input = getchar()) != '\'') + printf("unproper single quotes detected at line %d\n", line); + + continue; + } + + /* process all others */ + + count(input, &parentheses, &brackets, &braces); + } + } + + if (parentheses%=2) + printf("unproper parentheses detected\n"); + if (brackets%=2) + printf("unproper brackets detected\n"); + if (braces%=2) + printf("unproper braces detected\n"); + if (double_quotes == TRUE) + printf("unproper double quotes detected at line %d\n", double_quotes_line); + + return 0; +} + +void +count(char input, int* parentheses, int* brackets, int* braces) +{ + switch (input) { + case '(' : + (*parentheses)++; + break; + case ')' : + (*parentheses)--; + break; + case '[' : + (*brackets)++; + break; + case ']' : + (*brackets)--; + break; + case '{' : + (*braces)++; + break; + case '}' : + (*braces)--; + break; + } +} @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <limits.h> +#include <float.h> + +int +main(void) +{ + unsigned char c; + unsigned short s; + unsigned int i; + unsigned long l; + + c = ~0; + s = ~0; + i = ~0; + l = ~0; + + c >>= 1; + s >>= 1; + i >>= 1; + l >>= 1; + + /* signed values */ + + printf("limits char min: %20d, max: %20d\n", CHAR_MIN, CHAR_MAX); + printf("comput char min: %20d, max: %20d\n", -c-1, c); + + printf("limits short min: %20d, max: %20d\n", SHRT_MIN, SHRT_MAX); + printf("comput short min: %20d, max: %20d\n", -s-1, s); + + printf("limits int min: %20d, max: %20d\n", INT_MIN, INT_MAX); + printf("comput int min: %20d, max: %20d\n", -i-1, i); + + printf("limits long min: %20ld, max: %20ld\n", LONG_MIN, LONG_MAX); + printf("comput long min: %20ld, max: %20ld\n", -l-1, l); + + printf("limits float min: %20g, max: %20g\n", FLT_MIN, FLT_MAX); + printf("limits double min: %20g, max: %20g\n", DBL_MIN, DBL_MAX); + + /* unsigned values */ + + printf("limits uchar min: %20d, max: %20u\n", 0, UCHAR_MAX); + printf("comput uchar min: %20d, max: %20u\n", 0, c*2+1); + + printf("limits ushort min: %20d, max: %20u\n", 0, USHRT_MAX); + printf("comput ushort min: %20d, max: %20u\n", 0, s*2+1); + + printf("limits uint min: %20d, max: %20u\n", 0, UINT_MAX); + printf("comput uint min: %20d, max: %20u\n", 0, i*2+1); + + printf("limits ulong min: %20d, max: %20lu\n", 0, ULONG_MAX); + printf("comput ulong min: %20d, max: %20lu\n", 0, l*2+1); + + return 0; +} @@ -0,0 +1,20 @@ +#include <stdio.h> + +char lower(char intput); + +int +main(void) +{ + char intput; + + while((intput = getchar()) != EOF) + printf("%c", lower(intput)); + + return 0; +} + +char +lower(char intput) +{ + return (intput >= 'A' && intput <= 'Z') ? intput-'A'+'a' : intput; +} @@ -0,0 +1,65 @@ +#include <stdio.h> + +#define MAXLINE 1000 + +int gline(char str[], int len); +void cp_str(char to[], char from[]); + +int +main(void) +{ + int len, max; + char str[MAXLINE]; + char mstr[MAXLINE]; + + max = 0; + + while ((len = gline(str, MAXLINE)) > 0) + if (len > max) { + cp_str(mstr, str); + max = len; + } + + if (max > 0) + printf("\nlength: %d\n%s", max, mstr); + + return 0; +} + +int +gline(char str[], int len) +{ + int i; + char input; + + i = 0; + + while (i < len-1) { + input = getchar(); + + if (input == EOF) + break; + else if (input == '\n') + break; + + str[i] = input; + i++; + } + + + if (input == '\n' && i < len-1) { + str[i] = '\n'; + ++i; + } + + str[i] = '\0'; + + return i; +} + +void +cp_str(char to[], char from[]) +{ + for(int i =0; (to[i] = from[i]) != '\0'; i++) + ; +} @@ -0,0 +1,34 @@ +#include <stdio.h> + +int +main(void) +{ + char input; + int num; + + num = 0; + + while ((input = getchar()) != EOF) { + + if (input == '0') { + if ((input = getchar()) == 'x' || input == 'X') + continue; + else + input = '0'; + } + + if (!((input >= '0' && input <= '9') || (input >= 'a' && input <= 'f') || (input >= 'A' && input <= 'F'))) + continue; + else if (input >= '0' && input <= '9') + num = num * 16 + input - '0'; + else if (input >= 'a' && input <= 'f') + num = num * 16 + 10 + input - 'a'; + else if (input >= 'A' && input <= 'F') + num = num * 16 + 10 + input - 'A'; + + } + + printf("\n%d\n", num); + + return 0; +} @@ -0,0 +1,35 @@ +#include <stdio.h> + +/* function to delete each character in str1 that matches with str2 */ +char* squeeze(char str[], const char cs[]); + +int +main(void) +{ + char str[] = "ok chud you win this time"; + char del[] = "oi"; + + printf("%s\n", squeeze(str, del)); + + return 0; +} + +char* +squeeze(char str[], const char cs[]) +{ + int i, j, k; + + for (i = j = 0; str[i] != '\0'; i++) { + + for (k = 0; cs[k] != '\0'; k++) + if (str[i] == cs[k]) + break; + + if (cs[k] == '\0') + str[j++] = str[i]; + } + + str[j] = '\0'; + + return str; +} @@ -0,0 +1,32 @@ +#include <stdio.h> + +/* function to delete each character in str1 that matches with str2 */ +int any(const char s1[], const char s2[]); + +int +main(void) +{ + char str[] = "ok chud you win this time"; + char del[] = "ic"; + + printf("%d\n", any(str, del)); + + return 0; +} + +int +any(const char s1[], const char s2[]) +{ + int i, j; + + for (i = 0; s1[i] != '\0'; i++) { + + for (j = 0; s2[j] != '\0'; j++) + if (s2[j] == s1[i]) + return i+1; + + } + + return -1; +} + @@ -0,0 +1,47 @@ +#include <stdio.h> + +#define INT_NBITS 32 + +unsigned getbits(unsigned x, int from, int to); +void pbits(int x); +unsigned setbits(unsigned x, int from, int to, int source); + +int +main(void) +{ + int x = 423834583; + int y = 354543434; + + pbits(x); + pbits(y); + + pbits(setbits(x, 12, 5, y)); + + return 0; +} + +unsigned +getbits(unsigned x, int from, int to) +{ + return (x >> (from+1-to)) & ~(~(unsigned)0 << to); +} + +void +pbits(int x) +{ + for (int i = INT_NBITS-1; i >= 0; i--) { + if (i%4 == 3) + printf(" "); + + printf("%u", getbits(x, i, 1)); + } + + printf("\n"); +} + +unsigned +setbits(unsigned x, int from, int to, int source) +{ + int model = ((((unsigned)~0 << (INT_NBITS-from)) >> (INT_NBITS - (from+to))) << to); + return (x & ~model) | (source & model); +} @@ -0,0 +1,44 @@ +#include <stdio.h> + +#define INT_NBITS 32 + +unsigned getbits(unsigned x, int from, int to); +void pbits(int x); +unsigned invert(unsigned x, int from, int to); + +int +main(void) +{ + int x = 3423834583; + + pbits(x); + + pbits(invert(x, 32, 32)); + + return 0; +} + +unsigned +getbits(unsigned x, int from, int to) +{ + return (x >> (from+1-to)) & ~(~(unsigned)0 << to); +} + +void +pbits(int x) +{ + for (int i = INT_NBITS-1; i >= 0; i--) { + if (i%4 == 3) + printf(" "); + + printf("%u", getbits(x, i, 1)); + } + + printf("\n"); +} + +unsigned +invert(unsigned x, int from, int to) +{ + return x ^ ((((unsigned)~0 << (INT_NBITS-from)) >> (INT_NBITS-to)) << (from-to)); +} @@ -0,0 +1,55 @@ +#include <stdio.h> + +#define INT_NBITS 32 +#define ONE 1 + +unsigned getbits(unsigned x, int from, int to); +void pbits(int x); +unsigned rightrot(unsigned x, int n); + +int +main(void) +{ + int x = 2394234; + + pbits(x); + + pbits(rightrot(x, 8)); + + return 0; +} + +unsigned +getbits(unsigned x, int from, int to) +{ + return (x >> (from+1-to)) & ~(~(unsigned)0 << to); +} + +void +pbits(int x) +{ + for (int i = INT_NBITS-1; i >= 0; i--) { + if (i%4 == 3) + printf(" "); + + printf("%u", getbits(x, i, 1)); + } + + printf("\n"); +} + +unsigned +rightrot(unsigned x, int n) +{ + int model = (x >> n) << n; + for (int i = 1; i <= n; i++) { + int shift = n+1-i*2; + + if (shift >= 0) + model |= ((x & (ONE << (n-i))) >> shift); + else + model |= ((x & (ONE << (n-i))) << -shift); + } + + return model; +} @@ -0,0 +1,23 @@ +#include <stdio.h> + +int bitcount(unsigned num); + +int +main(void) +{ + int x = 1337; /* a random int */ + printf("%d\n", bitcount(x));; + + return 0; +} + +int +bitcount(unsigned num) +{ + int bits; + + for (bits = 0; num != 0; num &= (num-1)) + bits++; + + return bits; +} @@ -0,0 +1,56 @@ +#include <stdio.h> + +int binsearch(int x, int v[], int n); +int binsearch2(int x, int v[], int n); + +int +main(void) +{ + int v[10]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 11}; + printf("%d\n", binsearch(v[5], v, 10)); +} + +int +binsearch(int x, int v[], int n) +{ + int low, high, mid; + + low = 0; + high = n-1; + + while (low <= high) { + mid = (low+high)/2; + + if (x < v[mid]) + high = mid - 1; + else if (x > v[mid]) + low = mid + 1; + else + return mid; + } + + return -1; +} + +int +binsearch2(int x, int v[], int n) +{ + int low, high, mid; + + low = 0; + high = n-1; + + while (low < high) { + mid = (low+high)/2; + + if (x <= v[mid]) + high = mid; + else + low = mid+1; + + } + + /* here low == high */ + + return (x == v[low]) ? low : -1; +} @@ -0,0 +1,119 @@ +#include <stdio.h> + +#define MAXLEN 5000 + +char* escape(char to[], const char from[]); +char* etr(char to[], const char from[]); + +int +main(void) +{ + int i; + char input; + char str[MAXLEN]; + char e[MAXLEN]; + char r[MAXLEN]; + + for (i = 0; (input = getchar()) != EOF && i+1 < MAXLEN; ++i) + str[i] = input; + + str[i] = '\0'; + + + if (str[i-1] != '\n') + printf("\n"); + + printf("%s\n", escape(e, str)); + printf("%s\n", etr(r, e)); + + return 0; +} + +char* +escape(char to[], const char from[]) +{ + int i, j; + + for (i = j = 0; from[i] != '\0' && j+1 < MAXLEN; ++i) + switch (from[i]) { + case '\n' : + to[j++] = '\\'; + to[j++] = 'n'; + break; + case '\t' : + to[j++] = '\\'; + to[j++] = 't'; + break; + case '\a' : + to[j++] = '\\'; + to[j++] = 'a'; + break; + case '\b' : + to[j++] = '\\'; + to[j++] = 'b'; + break; + case '\f' : + to[j] = '\\'; + to[j++] = 'f'; + break; + case '\r' : + to[j++] = '\\'; + to[j++] = 'r'; + break; + case '\v' : + to[j++] = '\\'; + to[j++] = 'v'; + break; + default: + to[j++] = from[i]; + break; + } + + to[i] = '\0'; + + return to; +} + +char* +etr(char to[], const char from[]) +{ + int i, j; + + for (i = j = 0; from[i] != '\0'; ++i) { + if (from[i] == '\\') + switch (from[++i]) { + case 'n' : + to[j++] = '\n'; + break; + case 't' : + to[j++] = '\t'; + break; + case 'a' : + to[j++] = '\a'; + break; + case 'b' : + to[j++] = '\b'; + break; + case 'f' : + to[j++] = '\f'; + break; + case 'r' : + to[j++] = '\r'; + break; + case 'v' : + to[j++] = '\r'; + break; + default : + to[j++] = '\\'; + to[j++] = from[i]; + break; + } + + else + to[j++] = from[i]; + } + + to[j] = '\0'; + + return to; +} @@ -0,0 +1,57 @@ +#include <stdio.h> + +#define MAXLEN 15 + +void reverse(char str[]); +char* itoa(int n, char str[]); + +int +main(void) +{ + int n = -2147483648; + char str[MAXLEN]; + + printf("%d\n", n); + printf("%s\n", itoa(n, str)); + + return 0; +} + +void +reverse(char str[]) +{ + int i, j; + char temp; + + for (j = 0; str[j] != '\0'; j++) + ; + + for (i = 0, --j; i < j; i++, j--) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + } +} + +char* +itoa(int n, char str[]) +{ + int sign, i; + + if ((sign = n) < 0) + n = -n; + + i = 0; + do { + str[i++] = n%10 + '0'; + } while ((n /= 10) > 0); + + if (sign <0) + str[i++] = '-'; + + str[i] = '\0'; + + reverse(str); + + return str; +} |