Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

String functions in C?

LakshitLakshit Member
edited July 28 in Programming

I'm attempting to make my own string function and I am by all accounts stuck as of now. The following is what I have such a long way to make a string and print it and return the size. Yet, I really want 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.

#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;
}

I want to try the procedure described here but I'm scared if I tried wrong. Can someone give some insight here? Any help with this problem would be appreciated. Much Thanks!

Sign In or Register to comment.