fork download
  1. //structure for creating dictionary
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. struct trie
  7. {
  8. struct trie *arr[26];
  9. char *meaning;
  10. int flag;
  11. }*head;
  12. //initializing
  13. struct trie* init(struct trie *root)
  14. {
  15. root=malloc(sizeof(struct trie *));
  16. root->flag=-1;
  17. int i=0;
  18. for(i=0;i<26;i++)
  19. root->arr[i]=NULL;
  20. return root;
  21. }
  22. //inserting word and its meaning
  23. void insert(struct trie *root,char *word,char * mean)
  24. {
  25. int wordLen=strlen(word);
  26. int i;
  27. for(i=0;i<wordLen;i++)
  28. {
  29. int val=word[i]-'a';
  30. if(!(root->arr[val]))
  31. {
  32. struct trie *tmp;
  33. tmp=init(tmp);
  34. root->arr[val]=tmp;
  35. root=tmp;
  36. if(i==(wordLen-1))
  37. {
  38. root->flag=1;
  39. root->meaning=malloc(sizeof(char)*strlen(mean));
  40. strcpy(root->meaning,mean);
  41. break;
  42. }
  43. continue;
  44. }
  45. root=root->arr[val];
  46. if(i==(wordLen-1))
  47. {
  48. root->flag=1;
  49. root->meaning=malloc(sizeof(char)*strlen(mean));
  50. strcpy(root->meaning,mean);
  51. }
  52. }
  53. }
  54. //checking meaning of a word.
  55. char* findMeaning(struct trie *root,char *word)
  56. {
  57. int len=strlen(word);
  58. int i;
  59. for(i=0;i<len;i++)
  60. {
  61. int val=word[i]-'a';
  62. if(!(root->arr[val]))
  63. return NULL;
  64. root=root->arr[val];
  65. }
  66. if(root->flag==1)
  67. return root->meaning;
  68. return NULL;
  69. }
  70. int main()
  71. {
  72. head=init(head);
  73. insert(head,"me","you");
  74. printf("%s",findMeaning(head,"me")); //just testing
  75. return 0;
  76. }
Runtime error #stdin #stdout #stderr 0s 4552KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.