Advertisement
dmilicev

anagram_v1.c

May 21st, 2020
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     anagram_v1.c
  4.  
  5.     Anagram is a word or phrase formed by
  6.     rearranging letters of different word or phrase.
  7.     For example
  8.     "BRUSH" and "SHRUB",
  9.     "TALE" and "LATE"
  10.     are anagrams of each other.
  11.  
  12.     How to examine if it is an anagram?
  13.     If all the characters of one sting are in another string, it is an anagram.
  14.  
  15.     An elegant solution proposed by Steven Serrano
  16.     https://web.facebook.com/stvnsrrn18
  17.     is just to get the sum of characters in both words and equal it,
  18.     for every character in the ascii there's a decimal equivalent.
  19.  
  20.  
  21.     You can find all my C programs at Dragan Milicev's pastebin:
  22.  
  23.     https://pastebin.com/u/dmilicev
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28.  
  29. // returns 1 if word1 and word2 are anagrams, otherwise returns 0.
  30. // version without pointers
  31. int are_anagrams( char word1[], char word2[] )
  32. {
  33.     int i=0, sum1=0, sum2=0;
  34.  
  35.     while( word1[i] != '\0' )       // from the first to the last character of word1
  36.     {
  37.         sum1 = sum1 + (int)word1[i];
  38.         i++;                        // increment counter
  39.     }
  40.  
  41.     i = 0;  // reset counter
  42.     while( word2[i] != '\0' )       // from the first to the last character of word2
  43.     {
  44.         sum2 = sum2 + (int)word2[i];
  45.         i++;                        // increment counter
  46.     }
  47.  
  48.     if( sum1 == sum2 )              // if both sums are the same
  49.         return 1;
  50.     else
  51.         return 0;
  52. }
  53.  
  54. // returns 1 if word1 and word2 are anagrams, otherwise returns 0.
  55. // version with pointers
  56. int are_anagrams_v1( char *word1, char *word2 )
  57. {
  58.     int i=0, sum1=0, sum2=0;
  59.  
  60.     while( *(word1+i) != '\0' )     // from the first to the last character of word1
  61.     {
  62.         sum1 = sum1 + (int)*(word1+i);
  63.         i++;                        // increment counter
  64.     }
  65.  
  66.     i = 0;  // reset counter
  67.  
  68.     while( *(word2+i) != '\0' )     // from the first to the last character of word2
  69.     {
  70.         sum2 = sum2 + (int)*(word2+i);
  71.         i++;                        // increment counter
  72.     }
  73.  
  74.     if( sum1 == sum2 )              // if both sums are the same
  75.         return 1;
  76.     else
  77.         return 0;
  78. }
  79.  
  80. int main(void)
  81. {
  82.     char word1[] = "brush";
  83.     char word2[] = "shrub";
  84.  
  85.     if( are_anagrams(word1, word2) )
  86.         printf("\n %s and %s are anagrams. \n", word1, word2 );
  87.     else
  88.         printf("\n %s and %s are not anagrams. \n", word1, word2 );
  89.  
  90.  
  91.     if( are_anagrams_v1(word1, word2) )
  92.         printf("\n %s and %s are anagrams. \n", word1, word2 );
  93.     else
  94.         printf("\n %s and %s are not anagrams. \n", word1, word2 );
  95.  
  96.  
  97.     return 0;
  98.  
  99. } // main()
Advertisement
RAW Paste Data Copied
Advertisement