Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void removeTrash(char *str)
- {
- unsigned int shift = 0; // Сдвиг символов
- unsigned int size = 0; // Размер результата
- unsigned int index = 1; // Индекс в исходной строке
- char c = str[0]; // Текущий символ
- while (c != '\0') {
- if (c == ' ' || c == '\t') { // Встретили разделитель
- shift++; // Увеличиваем сдвиг
- } else {
- if (shift > 0) // Сдвиг есть?
- str[index - shift - 1] = c;
- size++; // +размер
- }
- c = str[index++]; // Новый символ и индекс
- }
- realloc(str, size + 1); // size + размер ноль-символа
- str[size] = '\0'; // Заверщшаем строку корректно
- }
- unsigned int read(char **array)
- {
- unsigned int index = 0; // Индекс
- while(1) {
- char *str = (char *)malloc(256); // Строка (динамически выделили в куче)
- gets(str); // Читаем
- if (str[0] == '.') // Считали .?
- break;
- removeTrash(str); // Поехали удалять и размер менять
- array[index++] = str; // Дописываем
- }
- return index; // Размер
- }
- void write(char **array, unsigned int size)
- {
- for (int i = 0; i < size; i++) {
- printf(array[i]);
- printf("\n");
- }
- }
- int main()
- {
- char **array = (char **)malloc(sizeof(char *) * 32);
- unsigned int size = read(array);
- realloc(array, sizeof(char *) * size); // Уменьшаем размер
- write(array, size); // Пошли печатать
- for (int i = 0; i < size; i++) { // Осторожно, очистка памяти
- free(array[i]);
- }
- free(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement