2015-03-20 18:20:44 +00:00
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* K&R implementation
|
|
|
|
*/
|
|
|
|
void int_to_ascii(int n, char str[]) {
|
|
|
|
int i, sign;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-09 10:05:15 +00:00
|
|
|
|
2015-03-20 18:20:44 +00:00
|
|
|
/* K&R */
|
2022-10-09 10:05:15 +00:00
|
|
|
void reverse(char s[]){
|
|
|
|
char tmp;
|
|
|
|
int l, j;
|
|
|
|
l = strlen(s);
|
|
|
|
for (j = 0; j < l/2; j++){
|
|
|
|
tmp = s[l - j - 1];
|
|
|
|
s[l - j - 1] = s[j];
|
|
|
|
s[j] = tmp;
|
2015-03-20 18:20:44 +00:00
|
|
|
}
|
2022-10-09 10:05:15 +00:00
|
|
|
}
|
2015-03-20 18:20:44 +00:00
|
|
|
|
|
|
|
/* K&R */
|
|
|
|
int strlen(char s[]) {
|
|
|
|
int i = 0;
|
|
|
|
while (s[i] != '\0') ++i;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(char s[], char n) {
|
|
|
|
int len = strlen(s);
|
|
|
|
s[len] = n;
|
|
|
|
s[len+1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void backspace(char s[]) {
|
|
|
|
int len = strlen(s);
|
|
|
|
s[len-1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* K&R
|
|
|
|
* Returns <0 if s1<s2, 0 if s1==s2, >0 if s1>s2 */
|
|
|
|
int strcmp(char s1[], char s2[]) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; s1[i] == s2[i]; i++) {
|
|
|
|
if (s1[i] == '\0') return 0;
|
|
|
|
}
|
|
|
|
return s1[i] - s2[i];
|
|
|
|
}
|