A collection of general-purpose utility functions in C for common operations related to math, strings, and arrays.
- Place
utility.candutility.hinto your project's source directory. - Include the header file in your code:
#include "utility.h"
- When compiling, remember to include
utility.cin the command:gcc your_main_file.c utility.c -o your_program
Here is a complete list of all available functions.
void swap(int *a, int *b): Swaps the values of two integers.int max(int a, int b): Returns the maximum of two integers.int min(int a, int b): Returns the minimum of two integers.unsigned long long factorial(int n): Calculates the factorial of a number.int is_prime(int n): Checks if a number is prime. Returns1if prime,0otherwise.
int string_length(const char *str): Calculates the length of a string.void reverse_string(char *str): Reverses a string in place.
void bubble_sort(int arr[], int size): Sorts an array in ascending order.int find_element(int arr[], int size, int target): Finds the first index of a target element. Returns-1if not found.int find_first_occurrence(int arr[], int size, int value): Finds the first index of a value. Returns-1if not found.int find_last_occurrence(int arr[], int size, int value): Finds the last index of a value. Returns-1if not found.
void reverse_array(int arr[], int size): Reverses the elements of an array in place.void copy_array(int src[], int dest[], int size): Copies elements from a source array to a destination array.int remove_duplicates(int arr[], int size): Removes duplicate elements from a sorted array and returns the new size.void fill_array(int arr[], int size, int value): Fills an array with a specific value.void rotate_array_left(int arr[], int size, int k): Rotates an array to the left bykpositions.void rotate_array_right(int arr[], int size, int k): Rotates an array to the right bykpositions.void concatenate_arrays(int arr1[], int size1, int arr2[], int size2, int dest[]): Concatenates two arrays into a destination array.void get_subarray(int arr[], int size, int start, int end, int dest[]): Creates a subarray from a start to an end index.void zero_array(int arr[], int size): Sets all elements of an array to zero.void merge_sorted_arrays(int arr1[], int size1, int arr2[], int size2, int dest[]): Merges two sorted arrays into a single sorted destination array.
int array_sum(int arr[], int size): Calculates the sum of all elements in an array.int array_max(int arr[], int size): Finds the maximum element in an array.int array_min(int arr[], int size): Finds the minimum element in an array.int is_sorted(int arr[], int size): Checks if an array is sorted in ascending order.int arrays_equal(int arr1[], int arr2[], int size1, int size2): Checks if two arrays are identical.double array_median(int arr[], int size): Calculates the median of a sorted array.int count_occurrences(int arr[], int size, int value): Counts how many times a value appears in an array.int array_contains(int arr[], int size, int value): Checks if an array contains a specific value.int is_array_palindrome(int arr[], int size): Checks if an array is a palindrome. =======
C language Utility functions!
9ac2b44084f18597751ddd061b0f2b79c3139faa