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 search function to the previous program. The function gives back the index of found, -1 if not!
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