Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdlib.h>
- /*
- Видаляємо букви, що повторюються та рахуємо довжину отриманого слова
- */
- bool check_word(char* word) {
- int index = 0;
- int word_without_duplicates_size = strlen(word);
- char* word_without_duplicates = malloc(sizeof(char) * word_without_duplicates_size);
- for (int i = 0; i < word_without_duplicates_size; i++) {
- int j;
- for (j = 0; j < i; j++) {
- if (word[i] == word[j])
- break;
- }
- if (j == i)
- word_without_duplicates[index++] = word[i];
- }
- int final_size = strlen(word_without_duplicates);
- free(word_without_duplicates);
- return final_size > 2;
- }
- /*
- Видалення працює шляхом заміщення елемента, який потрібно видалити з
- подальшим зсувом всіх елементів в масиві.
- Після цього зменшуємо значення довжини масиву на 1.
- (1 2 3 4 5) => (видалити елемент з індексом 3) => (1 2 3 5).
- */
- int delete_element_from_array(char** array_to_print, int index, int array_length) {
- for (int i = index; i < array_length - 1; i++) {
- array_to_print[i] = array_to_print[i+1];
- }
- return --array_length;
- }
- /*
- Проходимо по масиву та виводимо кожен елемент.
- */
- void print_array(char** array_to_print, int array_length) {
- for (int i = 0; i < array_length; i++) {
- printf("%s\n", array_to_print[i]);
- }
- }
- int main()
- {
- int n = 7;
- char* words_set[] = {"abc", "aa", "ac", "hello", "aabb", "afdsf", "mama"};
- char* words_array[n];
- // Ініціалізуємо масив слів, у якому n елементів
- for (int i = 0; i < n; i++) {
- words_array[i] = words_set[i];
- }
- print_array(words_array, n);
- puts("");
- // Проходимо по масиву слів, перевіряємо кожне слово,
- // за потреби виконуємо видалення.
- for (int i = 0; i < n; i++) {
- if (!check_word(words_array[i])) {
- printf("invalid word: %s\n", words_array[i]);
- n = delete_element_from_array(words_array, i, n);
- i -=1;
- }
- }
- printf("\nFINAL ARRAY LENGTH: %d\n", n);
- printf("\nFINAL ARRAY:\n");
- print_array(words_array, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement