Advertisement
Guest User

Anagram

a guest
Feb 21st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. /* header files */
  2. #include <stdio.h>      // get access to printf() and scanf()
  3. #include <stdbool.h>                                                    // get access to boolean values
  4. #include <string.h>                                                     // get access to strlen()
  5.  
  6. /* function prototypes */
  7. bool searchForChar(char c, char* small_word);
  8. bool isAnagram(char* small_word, char* large_word);
  9.  
  10. /* function definitions */
  11. int main(void)
  12. {
  13.     char* small_word;
  14.     char* large_word;                                                   // set variable for the 2 words
  15.    
  16.     printf("Small word is: ");                                          // ask for the small one
  17.     scanf("%s", small_word);
  18.    
  19.     printf("Large word is: ");                                          // ask for the large one
  20.     scanf("%s", large_word);
  21.    
  22.     char* result;
  23.    
  24.     if (isAnagram(small_word, large_word)) result = "match!";               // check and print the result
  25.     else result = "no matching!";
  26.    
  27.     printf("%s\n", result);
  28. }
  29.  
  30. /*  for example, we have "cat" and "actor".
  31.     we are going to divide "actor" into triplets like that (act, cto, tor)
  32.     then we will test matching between each triplet and the small word
  33.     if matching present our function will return (true) */
  34.  
  35. bool isAnagram(char* small_word, char* large_word)
  36. {
  37.     int len_of_small = strlen(small_word);                              // get the length of the small word
  38.  
  39.     int len_of_large = strlen(large_word);                              // get the length of the large one
  40.  
  41.     int counter;                                                        // set a counter to check full matching
  42.  
  43.     for (int i = 0; i <= len_of_large - len_of_small; i++)              // iterate over the large word till the index before the end by the length of the small one
  44.     {
  45.         counter = 0;                                                    // set the counter at zero
  46.        
  47.         for (int j = i; j < i + len_of_small; j++)                      // iterate over the large word from the current index (i) for the length of the small
  48.         {
  49.             if(searchForChar(large_word[j], small_word)) counter++;     // check if the current char is present in the small word and if so update counter
  50.         }
  51.  
  52.         if (counter == len_of_small )  return true;                     //  if counter reach the length of the small word return true
  53.     }
  54.  
  55.     return false;                                                       // else, return false
  56. }
  57.  
  58. bool searchForChar(char c, char* small_word)
  59. {
  60.     for (int i = 0, len = strlen(small_word); i < len; i++)             // iterate over the small word and see if the char sent from prev. function exists in it
  61.     {
  62.         if (small_word[i] == c)
  63.         {
  64.             return true;                                                // if so, return true
  65.         }
  66.     }
  67.  
  68.     return false;                                                       // else, return false
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement