목록Programming/Function (21)
MS Jeong - Blog
/* getBits: get n bits from position p */ unsigned getBits(unsigned int x, int p, int n) { return ((x >> (p + 1 - n)) & ~((~0)
/* strcat: concatenate t to end of s; s must be big enough */ void strcat(char s[], char t[]) { int i, j; i = j = 0; while (s[i++] != '\0') { /* find end of s */ ; } while ((s[i++] = t[j++]) != '\0') { /* copy t */ ; } return; }
/* squeeze: delete all c from s */ void squeeze(char s[], int c) { int i, j; for (i = j = 0; s[i] != '\0'; i++) { if (s[i] != c) { s[j++] = s[i]; } } s[j] = '\0'; return; }
/* lower: convert c to lower case; ASCII only */ int lower(int c) { if ((c >= 'A') && (c
/* atoi: convert s to integer */ int atoi(char s[]) { int i, n; n = 0; for (i = 0; ((s[i] >= '0') && (s[i]
/* strlen: return length of s */ int strlen(char s[]) { int i; i = 0; while (s[i++] != '\0') { ; } return i; }
/* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') { i++; } return; }
/* getLine: read a line into s, return length */ int getLine(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') { /* last character isn't '\n' the string length is longer than buffer */ s[i++] = c; } s[i] = '\0'; return i; }
/* raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i