#include <limits.h>
int strlen(const char* str) {
int len = 0;
while(*str++) len++;
return len;
}
void reverse(char* str, int left, int right) {
int i, len;
char temp;
len = (right + 1 - left) / 2;
for (i = 0; i < len; ++i) {
temp = str[left + i];
str[left + i] = str[right - i];
str[right - i] = temp;
}
}
void reverse_words(char* str) {
int len, i, word_start = 0;
len = strlen(str);
reverse(str, 0, len - 1);
for( i = 0; i < len; i++) {
if (str[i] == ' ') {
reverse(str, word_start, i - 1);
word_start = i + 1;
} else if ( i == len - 1) {
reverse(str, word_start, i);
}
}
}
int atoi(const char* str) {
char c;
int sign = 1, result = 0;
const int max = INT_MAX / 10;
if (*str == '-') {
sign = -1;
str++;
}
while((c = *str++)) {
if (result >= max) {
printf("Integer overflow!\n");
return 0;
}
result *= 10;
result += c - '0';
}
return result * sign;
}