Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- c_strings_with_pointers_with_dynamic_memory_allocation.c
- Updated: 2021.05.03 , by Dragan Milicev, https://www.facebook.com/dmilicev
- In C language string is actually a one-dimensional array of characters
- which is terminated by a null character '\0'.
- // returns the length of the string passed to it,
- // but not including the terminating null character '\0'.
- int my_strlen(char *str)
- // Copies the string pointed by source (including the null character) to the destination.
- // The function itself extends string1 to have enough space for string2.
- // Returns the copied string.
- void safe_strcpy(char* destination, const char* source)
- // String concatenation, concatenates string2 to string1.
- // The function itself extends string1 to have enough space to append string2.
- char *safe_strcat( char *string1, const char *string2 ){
- // Returns a pointer to the first occurrence of the character c in the string str,
- // or NULL if the character is not found.
- char *my_strchr(const char *s, int ch)
- // Returns index of the first occurrence of the character c in the string str,
- // or -1 if the character is not found.
- int my_strchr_index(const char *s, int ch)
- // Finds the first occurrence of the substr in the str.
- // The terminating '\0' characters are not compared.
- char *my_strstr( char *str, char *substr )
- // Inserts into string str char at position index pos_index
- char *insert_char_into_string( char *str, char ch, int pos_index )
- // Inserts into string subject string insert at position index pos_index
- char *insert_string_into_string( char *str, const char *insert, int pos_index ){
- // Compares strings str1 and str2. Returns value
- // > 0 if str1 is greater than st2,
- // = 0 if str1 is equal to str2,
- // < 0 if str1 is less than str2.
- int my_strcmp(const char *str1, const char *str2)
- Note:
- When strcmp compare the character c in string1 with the character C in string2.
- The character c is greater than the character C because the asscii value
- of character c is 99 and the asscii value of character C is only 67.
- // String str from right to left divides, using the character ch, into groups of n elements
- char *str_divide_into_groups( char *str, char ch, int n ) {
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h> // for malloc() and realloc()
- #include <string.h>
- // Returns the length of the string passed to it,
- // but not including the terminating null character '\0'.
- int my_strlen(char *str){
- char *first = str; //
- while( *str )
- str++;
- return str - first;
- }
- // Copies the string pointed by source (including the null character) to the destination.
- // Safe means that the function itself extends destination to have enough space for source.
- // Returns the copied string.
- char *safe_strcpy( char *destination, const char* source ){
- int i, j, destination_len=strlen(destination), source_len=strlen(source);
- // allocate sufficient space, reallocating memory size of destination for source
- destination = (char *)realloc( destination, (size_t)(source_len+1)*sizeof(char) );
- if( destination == NULL ) {
- fprintf(stderr, "\n\n destination realloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- while (*destination++ = *source++); // copying...
- *(destination+i) = '\0'; // finish the destination
- return destination;
- } // safe_strcpy()
- // Inserts into string str char at position index pos_index
- char *insert_char_into_string( char *str, char ch, int pos_index ) {
- int i, j, str_len=strlen(str), new_len;
- if( pos_index<0 || pos_index>str_len){
- fprintf(stderr, "\n\n Function insert_char_into_string(), pos_index = %d not valid ! \n\n", pos_index);
- return NULL;
- }
- new_len = str_len + 1; // +1 is for ch
- // allocate sufficient space, reallocating memory size, for exteneded str
- str = (char *)realloc(str, (size_t)(new_len+1)*sizeof(char) );
- if( str == NULL ) {
- fprintf(stderr, "\n\n str realloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- *(str+str_len) = '*'; // delete '\0' at the end of the string str
- *(str+new_len) = '\0'; // finish the extended string str on new_len
- i = str_len - 1; // index i points to the end of the string str
- j = new_len - 1; // index j points to the end of the extended string str
- while( i>=pos_index ) // from right to left until the pos_index is reached
- *(str+j--) = *(str+i--); // copy string characters and update indexes
- *(str+j--) = ch; // insert character ch on pos_index
- while( i>=0 ) // to the end of the string str
- *(str+j--) = *(str+i--); // copy string characters and update indexes
- return str;
- } // insert_char_into_string()
- // Inserts into string subject string insert at position index pos_index
- char *insert_string_into_string( char *str, const char *insert, int pos_index ){
- int i, j, str_len=strlen(str), k=strlen(insert), new_len=str_len+k;
- if( pos_index<0 || pos_index>str_len){
- fprintf(stderr, "\n\n Function insert_char_into_string(), pos_index = %d not valid ! \n\n", pos_index);
- return NULL;
- }
- // allocate sufficient space, reallocating memory size, for exteneded string str
- str = (char *)realloc(str, (size_t)(new_len+1)*sizeof(char) );
- if( str == NULL ) {
- fprintf(stderr, "\n\n str realloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- *(str+str_len) = '*'; // delete '\0' at the end of the string str
- *(str+new_len) = '\0'; // finish the extended string str on new_len
- i = str_len - 1; // index i points to the end of the string str
- j = new_len - 1; // index j points to the end of the extended string str
- k--; // k becomes an index
- while( j > pos_index+k ) // from right to left until the pos_index+k is reached
- *(str+j--) = *(str+i--); // copy string characters and update indexes
- while( k >= 0 ) // from right to left until the end of string insert
- *(str+j--) = *(insert+k--); // copy characters of string insert and update indexes
- while( i >= 0 ) // to the end of the string str
- *(str+j--) = *(str+i--); // copy string characters and update indexes
- return str;
- } // insert_string_into_string()
- // Compares strings str1 and str2. Returns value
- // > 0 if str1 is greater than st2,
- // = 0 if str1 is equal to str2,
- // < 0 if str1 is less than str2.
- int my_strcmp(const char *str1, const char *str2){
- while( *str1 ){
- if (*str1 != *str2) // if characters differ, or end of the second string is reached
- break;
- str1++; // move to the next pair of characters
- str2++;
- }
- // return the ASCII difference after converting `char*` to `unsigned char*`
- return *(const unsigned char*)str1 - *(const unsigned char*)str2;
- } // my_strcmp()
- // String concatenation, concatenates string2 to string1.
- // Safe means that the function itself extends string1 to have enough space to append string2.
- // Returns the concatenated string.
- char *safe_strcat( char *string1, const char *string2 ){
- int i, j, string1_len=strlen(string1), string2_len=strlen(string2);
- // allocate sufficient space, reallocating memory size, for exteneded string1
- string1 = (char *)realloc( string1, (size_t)(string1_len+string2_len+1)*sizeof(char) );
- if( string1 == NULL ) {
- fprintf(stderr, "\n\n string1 realloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- i = string1_len; // index i points to the '\0' at the end of the string1
- j = 0; // index j points to the begin of the string2
- while( *(string2+j) ) // for all string2
- *(string1+i++) = *(string2+j++);// after string1, copy string2 characters and update indexes
- *(string1+i) = '\0'; // finish the string1
- return (string1);
- } // safe_strcat()
- // Returns a pointer to the first occurrence of the character c in the string str,
- // or NULL if the character is not found.
- char *my_strchr(const char *s, int ch){
- do {
- if (*s == ch)
- return (char*)s;
- } while (*s++);
- return (0);
- } // my_strchr()
- // Returns index of the first occurrence of the character c in the string str,
- // or -1 if the character is not found.
- int my_strchr_index(const char *s, int ch){
- int i=0;
- do {
- if (*s == ch)
- return (i);
- i++;
- } while (*s++);
- return (-1);
- } // my_strchr_index()
- // Finds the first occurrence of the substr in the str.
- // The terminating '\0' characters are not compared.
- char *my_strstr( char *str, char *substr ){
- char *begin;
- char *pattern;
- while(*str){
- begin = str;
- pattern = substr;
- // if first character of substr match, check the whole str
- while( *str && *pattern && *str == *pattern ){
- str++;
- pattern++;
- }
- // if complete substr match, return starting address
- if( !*pattern )
- return begin;
- str = begin + 1; // increment main sting
- }
- return NULL;
- } // my_strstr()
- // Finds the first occurrence of the substr in the str.
- // The terminating '\0' characters are not compared.
- // Returns index of the first occurrence of the substr in the str,
- // or -1 if the substr is not found.
- int my_strstr_index( char *str, char *substr ){
- int i=0;
- char *begin;
- char *pattern;
- while(*str){
- begin = str;
- pattern = substr;
- // if first character of substr match, check the whole str
- while( *str && *pattern && *str == *pattern ){
- str++;
- i++;
- pattern++;
- }
- // if complete substr match, return starting index
- if( !(*pattern) )
- return ( i - strlen(substr) );
- str = begin + 1; // increment main sting
- i++;
- }
- return (-1);
- } // my_strstr_index()
- // String str from right to left divides, using the character ch, into groups of n elements
- char *str_divide_into_groups( char *str, char ch, int n ) {
- int i, j, str_len=strlen(str), new_len, counter, n_groups, n_rest, n_dots;
- if( n<=0 || n>str_len){
- fprintf(stderr, "\n\n Function divide_the_string_into_groups(), n = %d not valid ! \n\n", n);
- return NULL;
- }
- n_groups = str_len / n; // we calculate n_groups, number of groups of n elements
- n_rest = str_len % n; // we calculate n_rest, remaining elements after all groups
- if( n_rest == 0 ) // we calculate n_dots, number of dots
- n_dots = n_groups - 1;
- else
- n_dots = n_groups;
- new_len = str_len + n_dots; // we calculate new_len of extended string str
- // allocate sufficient space, reallocating memory size new_len, for exteneded str
- str = (char *)realloc(str, (size_t)(new_len+1)*sizeof(char) );
- if( str == NULL ) {
- fprintf(stderr, "\n\n str realloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- *(str+str_len) = '*'; // delete '\0' at the end of the string str
- *(str+new_len) = '\0'; // finish the extended string str on new_len
- i = str_len - 1; // index i points to the end of the string str
- j = new_len - 1; // index j points to the end of the extended string str
- while( i>=0 ){ // from end to beginning of string str
- while( n_groups>0 ){ // for all n_groups groups
- counter = 0; // reset counter for n elements in the group
- while(counter++ < n) // for all n elements in the group, count elements in group
- *(str+j--) = *(str+i--);// update indexes
- *(str+j--) = '.'; // after the group is completed we add a character ch (dot)
- n_groups--; // count groups
- }
- while( n_rest-- > 0 ) //for all remaining elements after all groups
- *(str+j--) = *(str+i--); // update indexes
- }
- return str;
- } // str_divide_into_groups()
- int main(void){
- char initial_string[]="abc1234567812345678";
- char initial_string2[]="ab1234567812345678";
- char ins_str[]="def";
- char ch='.';
- char ch1='3';
- char *str;
- char *str1;
- int index_of_position=3;
- int i;
- str1 = (char*)malloc( (strlen(initial_string)+1)*sizeof(char) ); // allocate sufficient space
- if( str1 == NULL ) {
- fprintf(stderr, "\n\n str1 malloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- printf("\n\n ------- my_strlen() ----------------------------------- \n\n");
- strcpy(str1,initial_string);
- printf("\n str1 = |%s| \n", str1);
- printf("\n my_strlen(str1) = |%d| \n", my_strlen(str1) );
- printf("\n\n ------- my_strchr_index() ----------------------------- \n\n");
- strcpy(str1,initial_string);
- printf("\n str1 = |%s| \n", str1);
- printf("\n ch1 = |%c| \n", ch1);
- i = my_strchr_index(str1, ch1);
- printf("\n i = |%d| \n", i);
- printf("\n String after |%c| is |%s| \n", ch1, str1+i );
- printf("\n\n ------- my_strchr() ----------------------------------- \n\n");
- strcpy(str1,initial_string);
- printf("\n str1 = |%s| \n", str1);
- printf("\n ch1 = |%c| \n", ch1);
- str1 = my_strchr(str1, ch1);
- printf("\n String after |%c| is |%s| \n", ch1, str1 );
- str = (char*)malloc( (strlen(initial_string)+1)*sizeof(char) ); // allocate sufficient space
- if( str == NULL ) {
- fprintf(stderr, "\n\n str malloc() error! \n\n");
- exit(EXIT_FAILURE);
- }
- printf("\n\n ------- safe_strcpy() --------------------------------- \n\n");
- *str = '\0'; // we empty str
- printf("\n destination = |%s| \n", str);
- printf("\n source = |%s| \n", "This is a string to be copied.");
- safe_strcpy(str, "This is a string to be copied.");
- printf("\n destination = |%s| \n", str);
- printf("\n\n ------- my_strstr() ----------------------------------- \n\n");
- strcpy(str,initial_string);
- printf("\n str = |%s| \n", str);
- strcpy(str1,"678");
- printf("\n str1 = |%s| \n", str1);
- printf("\n String after |%s| is |%s| \n", str1, my_strstr(str,str1) );
- printf("\n\n ------- my_strstr_index() ----------------------------- \n\n");
- strcpy(str,initial_string);
- printf("\n str = |%s| \n", str);
- strcpy(str1,"123");
- printf("\n str1 = |%s| \n", str1);
- i = my_strstr_index(str,str1);
- printf("\n index i = %d \n", i );
- printf("\n String after |%s| is |%s| \n", str1, str+i );
- printf("\n\n ------- safe_strcat() --------------------------------- \n\n");
- strcpy(str,initial_string);
- printf("\n str1 = |%s| \n", str);
- printf("\n str2 = |%s| \n", initial_string2);
- safe_strcat(str, initial_string2);
- printf("\n str1 + str2 = |%s| \n", str);
- printf("\n\n ------- insert_char_into_string() --------------------- \n\n");
- strcpy(str,initial_string);
- printf("\n Before, str = |%s| \n", str);
- insert_char_into_string(str, ch, index_of_position);
- printf("\n After ch, str = |%s| \n", str);
- printf("\n\n ------- insert_string_into_string() ------------------- \n\n");
- strcpy(str,initial_string);
- printf("\n Before, str = |%s| \n", str);
- printf("\n ins_str = |%s| \n", ins_str);
- printf("\n index_of_position = |%d| \n", index_of_position);
- insert_string_into_string(str, ins_str, index_of_position);
- printf("\n After, str = |%s| \n", str);
- printf("\n\n ------- my_strcmp() ----------------------------------- \n\n");
- strcpy(str,initial_string);
- strcpy(str1,initial_string2);
- printf("\n str = |%s| \n", str);
- printf("\n str1 = |%s| \n", str1);
- printf("\n my_strcmp() returns |%d| \n", my_strcmp(str,str1));
- if ( my_strcmp(str,str1) > 0 )
- printf("\n str is greater then str1. \n");
- if ( my_strcmp(str,str1) == 0 )
- printf("\n str is equal to str1. \n");
- if ( my_strcmp(str,str1) < 0 )
- printf("\n str is less then str1. \n");
- printf("\n\n ------- str_divide_into_groups() ---------------------- \n\n");
- strcpy(str,initial_string);
- printf("\n Before, str = |%s| \n", str);
- str_divide_into_groups(str, '.', 4);
- printf("\n After div, str = |%s| \n", str);
- printf("\n\n ------- str_divide_into_groups() ---------------------- \n\n");
- strcpy(str,initial_string2);
- printf("\n Before, str = |%s| \n", str);
- str_divide_into_groups(str, '.', 8);
- printf("\n After div, str = |%s| \n", str);
- printf("\n\n --------------------------------------------------------- \n\n");
- free(str); // we free up memory space because we used malloc()
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement