Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<string.h>
- #include <stdbool.h>
- #include "mobydick.h"
- /*
- Filename: mobydick.c
- Description: Your implementation of the mobydick anagram finder. Add comments whenever possible.
- */
- /* Global variables */
- char buffer[1250000] = {0};
- int len = 0;
- int value = 0; //number value on ASCI chart
- int *_line = 0;
- int equal = 0;
- /* Load stdin */
- void load_input()
- {
- char c;
- int i = 0;
- /* while c is not the end of the file*/
- while((c = getchar()) != EOF){
- //if the character is an alphabet, add it to the buffer
- if(isalpha(c) || isspace(c) || c == '\n'){
- if(isalpha(c)) c = tolower(c);
- buffer[i] = c;
- i++;
- }
- }
- }
- int checkeq(int a[], int b[], int length, int s, int e)
- {
- int i;
- for(i = s; i < e; i++){
- int g;
- for(g=0; g<length; g++){
- if(a[g] != b[i]) return 0;
- }
- }
- return 1;
- }
- void find_anagram(char *_unsolved_anagram, int *_lineno, char *_solved_anagram)
- {
- /* Your implementation */
- int i = 0;
- int alpha[26] = {0}; //variable that holds the number of each letter in the unsolved anagram
- printf("\n%s\n", _unsolved_anagram);
- printf("%zu\n",strlen(_unsolved_anagram));
- len = (int)strlen(_unsolved_anagram);
- //COUNTING THE OCCURENCE OF EACH LETTER IN THE UNSOLVED ANAGRAM
- for(i = 0; i < len; i++){
- if(isalpha(_unsolved_anagram[i])){
- _unsolved_anagram[i] = tolower(_unsolved_anagram[i]); //make sure all characters are lower case
- value =_unsolved_anagram[i] - 'a';
- printf("%d ", value);
- } else if(isspace(_unsolved_anagram[i])) value = 100; //prevents adding 1 to alpha[value] with value from last loop, spaces were causing miscounts
- if((value < 26) && (value >= 0)) alpha[value]++;
- //occurrence of that letter is increased by 1
- }
- //SEARCH BUFFER ARRAY FOR A MATCH TO ALPHA ARRAY
- int j;
- int mvalue = 0;
- int m[26] = {0};
- int start = 0;
- int end = len;
- do{
- for(j = start; j < end; j++){
- if(isalpha(buffer[j])){
- mvalue = buffer[j] - 'a';
- } else if(isspace(buffer[j])) value = 100;
- if((mvalue < 26) && (mvalue >= 0)) m[mvalue]++;
- }
- // While a match is not found -->
- // If the arrays the hold the number of occurences of each letter are equal:
- // 1) Use start and end variables to determine which elements in buffer to use. Take the contents of those elements
- // and copy them to _solved_anagram array
- // 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)
- equal = checkeq(alpha, m, len, start, end);
- if(equal==1){
- //IMPLEMENTATION OF TASK 1
- int b;
- for(b = start; b < end; b++){
- _solved_anagram[i] = buffer[b];
- }
- }
- // IMPLEMENTATION OF TASK 2
- int j;
- for(j = 0; j <= start; j++) {
- if(buffer[j] == '\n') _line++;
- }
- _lineno = _line;
- // If the m and alpha are not equal:
- // 1) Increment start variable by 1 and decrement the count of the letter from previous element
- // 2) Increment end variable by 1 and add the count of letter in that element to m
- } else if(equal==0){
- //IMPLEMENTATION OF TASK 1
- start++;
- mvalue = buffer[start - 1] - 'a';
- if(isalpha(buffer[start-1])){
- if(mvalue > 0 && mvalue <= 26) m[mvalue]--;
- }
- //IMPLEMENTATION OF TASK 2
- end++;
- mvalue = buffer[end] - 'a';
- if(isalpha(buffer[end])){
- if(mvalue > 0 && mvalue <= 26) m[mvalue]++;
- }
- }
- } while(equal==0);
- //need to clear alpha array so doesn't keep adding letters
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement