I have implemented few functions in C language that manipulate strings. These functions are not present in the standard C library.
string_library.c
#include <stdio.h>
#include <stdlib.h>
#include "string_library.h"
/*
* get_input_from_stdin_and_discard_extra_characters(char *str, long size):
*
* Function get_input_from_stdin_and_discard_extra_characters() reads at most
* 'size - 1' characters into 'str' from stdin and then appends the NULL
* character ('\0'). In all cases, reading input stops after encountering a
* newline ('\n') or EOF even if 'size - 1' characters have not been read.
* If a newline ('\n') or EOF is read then these are replaced by NULL character
* ('\0'). If there are extra characters in input, they are read and discarded.
* In all cases, str is returned.
*
*/
char *get_input_from_stdin_and_discard_extra_characters(char *str, long size)
{
char c = 0;
long num_chars_to_read = size - 1;
long i = 0;
if (!str)
return NULL;
if (num_chars_to_read <= 0)
return NULL;
for (i = 0; i < num_chars_to_read; i++) {
c = getchar();
if ((c == '\n') || (c == EOF)) {
str[i] = 0;
return str;
}
str[i] = c;
} // end of for loop
str[i] = 0;
// discard rest of input
while ((c = getchar()) && (c != '\n') && (c != EOF));
return str;
} // end of get_input_from_stdin_and_discard_extra_characters
int is_string_empty(const char *str)
{
if (!str)
return 1;
if (*str == 0)
return 1;
return 0;
} // end of is_string_empty
int is_string_all_whitespace(const char *str)
{
char *temp = (char *)(str);
if (is_string_empty(str))
return 1;
while (*temp) {
if ((*temp != ' ') && (*temp != '\t'))
return 0;
temp++;
}
return 1;
} // is_string_all_whitespace
/*
* This function fills 'str' with 'len - 1' random characters and appends a null
* byte at the end and returns 'str'.
*/
char *get_random_string(char *str, long len)
{
int min_num = 32;
int max_num = 126;
int i = 0;
for (i = 0; i < (len - 1); i++) {
str[i] = (random() % (max_num - min_num + 1)) + min_num;
}
str[len - 1] = 0;
return str;
}
/*
* str_replace_chr(char *str, const char orig, const char new):
*
* Function str_replace_chr() replaces all occurrences of 'orig' character in
* the string 'str' with the 'new' character. If 'str' is NULL then nothing is
* done. In all cases, 'str' is returned.
*
*/
char *str_replace_chr(char *str, const char orig, const char new)
{
char *orig_str = (str);
if (!str)
return orig_str;
while (*str) {
if (*str == orig)
*str = new;
str++;
}
return orig_str;
} // end of str_replace_chr
/*
* substr(const char *str, long start_index, long end_index):
*
* Function substr() allocates memory and returns a pointer to a string / character
* array which is a substring of 'str' starting from index 'start_index' till
* 'end_index' (inclusive). This substring is terminated by NULL byte at the end.
* If 'str' is NULL or 'start_index' is less than 0 or 'end_index' is less than 0
* or 'end_index' is less than 'start_index' then NULL is returned.
*
* The returned pointer points to a memory region containing the substring and this
* memory region was allocated using malloc. So, it is the user's responsibility to
* free the allocated memory.
*
*/
char *substr(const char *str, long start_index, long end_index)
{
char *substring = NULL;
long length = 0;
long i = 0;
if (!str)
return NULL;
if ((start_index < 0) || (end_index < 0) || (end_index < start_index))
return NULL;
length = end_index - start_index + 1;
substring = malloc(length + 1); // extra 1 byte for NULL byte
if (!substring)
return NULL;
for (i = 0; i < length; i++) {
substring[i] = str[i + start_index];
}
substring[i] = 0;
return substring;
} // end of substr