Я включаю весь код, поэтому, если у вас есть какие-либо комментарии, я хотел бы узнать!
Функция получает строку и создает массив строковых указателей; каждая строка содержит слово, которое начинается с выбранной буквы.
Моя проблема в том, что при функции free_string()
я получаю ошибку!
Я знаю, что это способ освободить матрицу, так что, может быть, что-то пошло не так еще до этого?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[SIZE],leteer,**string;
int size=0;
printf("please enter the string\n");
gets(str);
printf("please enter the letter\n");
scanf("%c",&leteer);
string=create_strings_from_letter(str, leteer, &size);
print_string(string,size);
free_string(string,size);
}
char ** create_strings_from_letter(char *str, char letter, int * size)
{
char **string_of_letters;
char *read = str,*write;
int count=0,i=0;
while (*read)//cheaking how much strings to allocate//
{
if (*read == tolower(letter)|| *read == toupper(letter))
{
(*size)++;
for (; *read && *read != ' '; read++);
}
for (; *read && *read != ' '; read++);
read++;
}
string_of_letters = (char**)malloc((*size)*sizeof(char*));
read = str;
while (*read)
{
for (; *read && *read != tolower(letter) && *read != toupper(letter);read++);
if (*read)
{
write = read;
for (; *read && *read != ' '; read++, count++);
string_of_letters[i] = (char*)malloc((count) * sizeof(char));
strncpy(string_of_letters[i], write,count);
string_of_letters[i][count] = '\0';
count = 0;
i++;
}
else
break;
}
return string_of_letters;
}
void free_string(char ** string, int size)
{
int i;
for (i = 0;i<size; i++)
free(string[i]);
free(string);
}