23char*
substring(
const char*
string,
int startPosition,
int length) {
24 if (startPosition < 0 || length < 0 || startPosition + length > strlen(
string)) {
28 char* sub = (
char*)malloc(length + 1);
32 strncpy(sub,
string + startPosition, length);
39 int strLen = strlen(
string);
40 if(startPosition < 0 || startPosition >= strLen)
43 for(
int i = startPosition; i < strLen; i++)
44 if(
string[i] == character)
45 return i - startPosition;
50int count(
const char*
string,
char character,
int startPosition) {
51 int strLen = strlen(
string);
52 if(startPosition < 0 || startPosition >= strLen)
56 for(
int i = startPosition; i < strLen; i++)
57 if(
string[i] == character)
63bool containsFrom(
const char*
string,
char character,
int startPosition) {
64 int strLen = strlen(
string);
65 if(startPosition < 0 || startPosition >= strLen)
68 for(
int i = startPosition; i < strLen; i++)
69 if(
string[i] == character)
76 if(strcmp(
string, subString) == 0)
79 int strLen = strlen(
string);
80 int subStrLen = strlen(subString);
82 if(startPosition < 0 || startPosition >= strLen || subStrLen == 0 || subStrLen > strLen - startPosition)
85 for(
int i = startPosition; i < strLen; i++)
86 if(
string[i] == subString[0] && i + subStrLen < strLen)
87 for(
int j = 0; j < subStrLen; j++) {
88 if(
string[i + j] != subString[j])
90 else if(j == subStrLen - 1)
98 return character >=
'0' && character <=
'9';
102 return isDecimalDigit(character) || (character >=
'a' && character <=
'f') || (character >=
'A' && character <=
'F');
116 (int)(digit -
'a') + 10 :
117 (int)(digit -
'A') + 10
126 for(
int i = 0; i < strlen(number); i++) {
139 return number == 0 ? 1 : (int)log10(abs(number)) + 1;
#define EXIT_ALLOC_FAILURE
Exit code when dynamic memory allocation fails.
bool isHexDigit(char character)
Checks if a char is a hexadecimal digit (not case sensitive).
bool containsFrom(const char *string, char character, int startPosition)
Checks if there is an occoruncy of the passed char from the passed position in the passed string.
int decimalDigitToInt(char digit)
If is a decimal digit, converts it to int; Otherwise returns ASCII integer representation of the pass...
int count(const char *string, char character, int startPosition)
Counts the number of occoruncies in the string, from a passed position, of the passed char.
int evalutateBase(char *number)
Evalutates the base of the number (by finding the biggest digit up to hexadecimal digit F) and return...
bool isDecimalDigit(char character)
Checks if a char is a decimal digit.
int offsetFromNext(const char *string, char character, int startPosition)
Counts the offset in the string between the position and the next occoruncy of the passed char.
int nDigits(int number)
Evalutates the number of digits of a number. The time complexity of this function is constant (O(1)).
char * substring(const char *string, int startPosition, int length)
Copies a substring (heap-allocated) from the passed string. If fails when allocating memory for the s...
int hexDigitToInt(char digit)
If is a hexadecimal digit (not case sensitive), converts it to int; Otherwise returns ASCII integer r...
bool containsSubstringFrom(const char *string, const char *subString, int startPosition)
Checks if there is an occoruncy of the passed substring from the passed position in the passed string...
Definition of utility functions.