Complementary task for topic: 8

M Nemeth · 2023-08-29 15:21:04.629218'

Dynamic arrays: Array of dynamic strings: search

Dynamic arrays: Array of dynamic strings: search

Add a sort function to the previous program. That sort the words in alphabetical order. The array must change, not a copy!

Hint:

Solution
#include 
void clear_buffer(){
    while((getchar())!='\n'){}
}

void Add_Word(char*** Arr,int* size){
    char* new_word=NULL;
    int len=1;
    char c;
    printf("give a new word:\n");
    while((c=getchar())!='\n'){
        new_word=(char *)realloc(new_word,(len+1)*sizeof(char));
        new_word[len-1]=c;
        new_word[len]='\0';
        len++;
    }
    *Arr=(char **)realloc(*Arr,((*size)+1)*sizeof(char*));

    (*Arr)[*size]=new_word; //precedence
    (*size)++;

    return; //No free needed because of realloc!

    }
 int search_word(char** Arr, int size, char* what){
    int index=-1;
    int i=0;
    while(i 0) {
                char* temp = strings[i];
                strings[i] = strings[i + 1];
                strings[i + 1] = temp;
                swapped = 1;
            }
        }
    } while (swapped);
}



int main()
{
    int size=0;
    char** Arr=NULL;
    int max_size;
    printf("how may words?");
    scanf("%d",&max_size);
    clear_buffer();
    for(int i=0;i
Explanation

< < previous    next > >