Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
utility.c File Reference

Implementation of utility functions. More...

#include "utility.h"

Go to the source code of this file.

Functions

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 substring, it exits with EXIT_ALLOC_FAILURE exit code.
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 count (const char *string, char character, int startPosition)
 Counts the number of occoruncies in the string, from a passed position, of the passed char.
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.
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.
bool isDecimalDigit (char character)
 Checks if a char is a decimal digit.
bool isHexDigit (char character)
 Checks if a char is a hexadecimal digit (not case sensitive).
int decimalDigitToInt (char digit)
 If is a decimal digit, converts it to int; Otherwise returns ASCII integer representation of the passed char.
int hexDigitToInt (char digit)
 If is a hexadecimal digit (not case sensitive), converts it to int; Otherwise returns ASCII integer representation 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 returns it. If the string contains a non-digit char (up to hexadecimal digit F), returns -1.
int nDigits (int number)
 Evalutates the number of digits of a number. The time complexity of this function is constant (O(1)).

Detailed Description

Implementation of utility functions.

Definition in file utility.c.

Function Documentation

◆ containsFrom()

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.

Parameters
stringThe string to check in.
characterThe char to look for.
startPositionThe position from which starts.
Returns
True if an occoruncy is found, false otherwise.

Definition at line 63 of file utility.c.

◆ containsSubstringFrom()

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.

Parameters
stringThe string to check in.
subStringThe substring to look for.
startPositionThe position from which starts.
Returns
True if an occoruncy is found, false otherwise.

Definition at line 75 of file utility.c.

◆ count()

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.

Parameters
stringThe string to count from.
characterThe char to count on the occoruncies in the string.
startPositionThe start position in the string to count from.
Returns
Returns the number of occoruncies of the char in the string. If there are not occoruncies of the char in the passed string, returns 0.

Definition at line 50 of file utility.c.

◆ decimalDigitToInt()

int decimalDigitToInt ( char digit)

If is a decimal digit, converts it to int; Otherwise returns ASCII integer representation of the passed char.

Parameters
digitThe char to convert.
Returns
The digit as an integer if the char is a decimal digit, ASCII integer representation of the passed char otherwise.

Definition at line 105 of file utility.c.

◆ evalutateBase()

int evalutateBase ( char * number)

Evalutates the base of the number (by finding the biggest digit up to hexadecimal digit F) and returns it. If the string contains a non-digit char (up to hexadecimal digit F), returns -1.

Parameters
numberThe number in text format.
Returns
The base of the number, if it is valid. Otherwise -1.

Definition at line 123 of file utility.c.

◆ hexDigitToInt()

int hexDigitToInt ( char digit)

If is a hexadecimal digit (not case sensitive), converts it to int; Otherwise returns ASCII integer representation of the passed char.

Parameters
digitThe char to convert.
Returns
The digit as an integer if the char is a hexadecimal giti, ASCII integer representation of the passed char otherwise.

Definition at line 109 of file utility.c.

◆ isDecimalDigit()

bool isDecimalDigit ( char character)

Checks if a char is a decimal digit.

Parameters
characterThe char to check.
Returns
True if the char is decimal digit, false otherwise.

Definition at line 97 of file utility.c.

◆ isHexDigit()

bool isHexDigit ( char character)

Checks if a char is a hexadecimal digit (not case sensitive).

Parameters
characterThe char to check.
Returns
True if the char is a hexadecima digit, false otherwise.

Definition at line 101 of file utility.c.

◆ nDigits()

int nDigits ( int number)

Evalutates the number of digits of a number. The time complexity of this function is constant (O(1)).

Parameters
numberThe number to evalutate.
Returns
The number of digits.

Definition at line 138 of file utility.c.

◆ offsetFromNext()

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.

Parameters
stringThe string where to search the occoruncy.
characterThe char to search to.
startPositionThe position where to start
Returns
The offset from the start position to the next occorncy of the char. Otherwise, if no more occoruncies occur, returns -1.

Definition at line 38 of file utility.c.

◆ substring()

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 substring, it exits with EXIT_ALLOC_FAILURE exit code.

Parameters
stringString to copy from.
startPositionThe position in str to start to copy from.
lengthThe length of the substring.
Returns
The substring (heap-allocated).

Definition at line 23 of file utility.c.