apkim221
apkim221

Reputation: 121

How to create my own String functions in C?

I am trying to create my own string function and I seem to be stuck at this point. Below is what I have so far to make a string and print it and return the size. But I need to make a function that is passed the pointer to a string of chars as well as a char and it returns the number of occurrences of that char in the string. I am trying to call the function make_string from this function but can't get it to work. The .h file just has the functions pre-listed. Any help with this would be appreciated. Thanks!

#include "readLineUtilities.h"

int make_string(char **line)
{
char *a,b;
int i,size=0;
a = (char *) malloc(sizeof(char));
b = getchar();
while( b != '\n' && size < MAX)
{       
    *(a+size) = b;  //remember at this point size = 0
    size++;
    b = getchar();
    a = realloc(a,size+1);
}
*(a+size) = '\0';  //end of string marker so no need to return the size directly
*line = a;
return size;    

}

int char_in_string (char *line, char c) {

make_string(*line);

}

void print_string(char *line, int size){
char *a;
int i;
a = line;
for (i=0;i<size;i++)
    printf("%c",*(a+i)); //no end of line
printf("\n");
}


int length_string(char *line){
int size = 0;
char *c,b;
c = line;
if ( c == NULL){ 
    printf("line is null\n");
    return size;
}
while (*(c + size) != '\0'){
    size++;
}
return size;
}

Upvotes: 0

Views: 2353

Answers (2)

autistic
autistic

Reputation: 15642

By looking at the collection of errors presented in this code, it's easy to form reasons why programs that use this code might not function as expected.

a = (char *) malloc(sizeof(char)); // sizeof (char) is always 1, because sizeof (type) tells you how many characters in type. Which type does malloc return? It's not necessary to cast void * to char *, because that conversion is provided implicitly by C: a = malloc(1); If you have errors that go away when you cast, you're missing a #include <stdlib.h> which is necessary for malloc/realloc to function.

b = getchar(); // Which type does getchar return? What is the type of b? These types should be the same, but in your example they're not. getchar returns an int, which will be an unsigned char value upon success. Upon failure, getchar will return a negative value, which is distinct from any possible successful value. I suggest storing the return value of getchar() into an int and verifying that it's positive before continuing. Otherwise, your program might appear to hang and bogart resources when getchar() returns errors that are distinct from the '\n' value.

a = realloc(a,size+1); // What happens if realloc returns NULL? Your program leaks the old allocation. I suggest assigning the return value to a char *temp, and checking for success before overwriting a. Your program might segfault occasionally if you don't fix this one.

int char_in_string (char *line, char c) { make_string(*line); } // Where is your return statement? make_string expects a char **, but the expression *line evaluates to a char. What is the type of line? What about &line? This would definitely cause a segfault when used.

I presume you're placing a '\0' at the end of the string in make_string for the purpose of determining where the string ends. I suggest stopping at the '\0' in your print_string implementation. If you attempt to use uninitialised values, you're invoking undefined behaviour and your program might crash.

Upvotes: 1

Stephen
Stephen

Reputation: 2843

Something like the following would work.

int char_in_string (char *line, char c) {

    int len = make_string(&line);
    int i, cnt = 0;
    for ( i = 0; i < len; ++i )
    {
        if ( line[i] == c ) ++cnt;
    }
    return cnt;

}

Upvotes: 1

Related Questions