我想創建我自己的字符串函數,我似乎被卡在這一點。以下是我迄今爲止製作一個字符串並打印並返回大小的內容。但我需要做一個函數,該函數將指針傳遞給一個字符串以及一個char,並返回該字符串中該char的出現次數。我試圖從這個函數調用函數make_string,但不能讓它工作。 .h文件只有預先列出的功能。任何幫助,這將不勝感激。謝謝!如何在C中創建自己的字符串函數?
#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;
}
你能解釋一下爲什麼你試圖這樣做嗎?它不能避免使用內置的字符串函數,因爲你使用'printf()'和'getchar()'。它不可能是性能,因爲的realloc的'數()'叫你'make_string使()'是荒謬的。那麼你想要做什麼? – 2013-02-19 03:06:53