Advertisement
Guest User

find anagram in book

a guest
Oct 6th, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #include<string.h>
  5. #include <stdbool.h>
  6. #include "mobydick.h"
  7.  
  8. /*
  9.         Filename: mobydick.c
  10.         Description: Your implementation of the mobydick anagram finder. Add comments whenever possible.
  11. */
  12.  
  13.  
  14. /* Global variables */
  15. char buffer[1250000] = {0};
  16. int len = 0;
  17. int value = 0; //number value on ASCI chart
  18. int *_line = 0;
  19. int equal = 0;
  20.  
  21. /* Load stdin */
  22. void load_input()
  23. {
  24.         char c;
  25.         int i = 0;
  26.  
  27.         /* while c is not the end of the file*/
  28.         while((c = getchar()) != EOF){
  29.                 //if the character is an alphabet, add it to the buffer
  30.                 if(isalpha(c) || isspace(c) || c == '\n'){
  31.                         if(isalpha(c)) c = tolower(c);
  32.                         buffer[i] = c;
  33.                         i++;
  34.                 }
  35.  
  36.         }
  37.  
  38. }
  39.  
  40. int checkeq(int a[], int b[], int length, int s, int e)
  41. {
  42.         int i;
  43.         for(i = s; i < e; i++){
  44.                 int g;
  45.                 for(g=0; g<length; g++){
  46.  
  47.                         if(a[g] != b[i]) return 0;
  48.                 }
  49.  
  50.         }
  51.         return 1;
  52. }
  53.  
  54. void find_anagram(char *_unsolved_anagram, int *_lineno, char *_solved_anagram)
  55. {
  56.         /* Your implementation */
  57.         int i = 0;
  58.         int alpha[26] = {0}; //variable that holds the number of each letter in the unsolved anagram
  59.  
  60.         printf("\n%s\n", _unsolved_anagram);
  61.  
  62.         printf("%zu\n",strlen(_unsolved_anagram));
  63.  
  64.         len = (int)strlen(_unsolved_anagram);
  65.  
  66.         //COUNTING THE OCCURENCE OF EACH LETTER IN THE UNSOLVED ANAGRAM
  67.         for(i = 0; i < len; i++){
  68.  
  69.  
  70.                 if(isalpha(_unsolved_anagram[i])){
  71.  
  72.                         _unsolved_anagram[i] = tolower(_unsolved_anagram[i]);   //make sure all characters are lower case
  73.                         value =_unsolved_anagram[i] - 'a';
  74.                         printf("%d ", value);
  75.  
  76.  
  77.                 } else if(isspace(_unsolved_anagram[i])) value = 100;   //prevents adding 1 to alpha[value] with value from last loop, spaces were causing miscounts
  78.  
  79.  
  80.                 if((value < 26) && (value >= 0)) alpha[value]++;
  81.                 //occurrence of that letter is increased by 1
  82.  
  83.         }
  84.  
  85.         //SEARCH BUFFER ARRAY FOR A MATCH TO ALPHA ARRAY
  86.  
  87.         int j;
  88.         int mvalue = 0;
  89.         int m[26] = {0};
  90.         int start = 0;
  91.         int end = len;
  92.  
  93.         do{
  94.  
  95.         for(j = start; j < end; j++){
  96.  
  97.                 if(isalpha(buffer[j])){
  98.  
  99.                         mvalue = buffer[j] - 'a';
  100.  
  101.                 } else if(isspace(buffer[j])) value = 100;
  102.  
  103.                 if((mvalue < 26) && (mvalue >= 0)) m[mvalue]++;
  104.  
  105.         }
  106.  
  107.         // While a match is not found -->
  108.         // If the arrays the hold the number of occurences of each letter are equal:
  109.         // 1) Use start and end variables to determine which elements in buffer to use. Take the contents of those elements
  110.         //      and copy them to _solved_anagram array
  111.         // 2) To determine line number, add up all the occurrences of '\n' up to the element num that represents the start of the solved anagram (start variable)
  112.  
  113.  
  114.         equal = checkeq(alpha, m, len, start, end);
  115.  
  116.         if(equal==1){
  117.  
  118.                 //IMPLEMENTATION OF TASK 1
  119.                 int b;
  120.                 for(b = start; b < end; b++){
  121.  
  122.                                 _solved_anagram[i] = buffer[b];
  123.                         }
  124.  
  125.                 }
  126.  
  127.                 // IMPLEMENTATION OF TASK 2
  128.                 int j;
  129.                 for(j = 0; j <= start; j++) {
  130.  
  131.                         if(buffer[j] == '\n') _line++;
  132.                 }
  133.  
  134.                 _lineno = _line;
  135.  
  136.         // If the m and alpha are not equal:
  137.         // 1) Increment start variable by 1 and decrement the count of the letter from previous element
  138.         // 2) Increment end variable by 1 and add the count of letter in that element to m
  139.         } else if(equal==0){
  140.  
  141.                 //IMPLEMENTATION OF TASK 1
  142.                 start++;
  143.  
  144.                 mvalue = buffer[start - 1] - 'a';
  145.  
  146.                 if(isalpha(buffer[start-1])){
  147.                         if(mvalue > 0 && mvalue <= 26) m[mvalue]--;
  148.                 }
  149.                 //IMPLEMENTATION OF TASK 2
  150.                 end++;
  151.  
  152.                 mvalue = buffer[end] - 'a';
  153.                 if(isalpha(buffer[end])){
  154.                         if(mvalue > 0 && mvalue <= 26) m[mvalue]++;
  155.                 }
  156.  
  157.         }
  158.  
  159.         } while(equal==0);
  160.  
  161.         //need to clear alpha array so doesn't keep adding letters
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement