Advertisement
bkit4s0

[hash 'a' str] finished

Jan 1st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAX 10
  5. void split(char s[], char s1[], char s2[])
  6. {
  7.         int l, m;
  8.         int i,j;
  9.         l = strlen(s);
  10.         m = l/2;
  11.         for (i = 0; i < m; ++i)
  12.         {
  13.                 s1[i] = s[i];
  14.                 //s2[i] = s[l - m + i]; // can't use to split s2
  15.         }
  16.         s1[i] = '\0';
  17.         for (i = m; i < l; ++i)
  18.         {
  19.                 s2[i-m] = s[i];
  20.         }
  21.         s2[i-m] = '\0';
  22. }
  23. int numofA(char s[])
  24. {
  25.         int count;
  26.         count = 0;
  27.         for (int i = 0; i < strlen(s); ++i)
  28.         {
  29.                 if(s[i] == 'A')
  30.                         count++;
  31.         }
  32.         return count;
  33. }
  34. int max(int x, int y)
  35. {
  36.         int result;
  37.         if(x > y)
  38.                 result = x;
  39.         else
  40.                 result = y;
  41.         return result;
  42. }
  43. int hash(char s[])
  44. {
  45.         int result;
  46.         char s1[MAX], s2[MAX];
  47.         result = numofA(s);
  48.         if(strlen(s) > 1)
  49.         {
  50.                 split(s,s1,s2);
  51.                 result += max(hash(s1),hash(s2));
  52.         }
  53.         return result;
  54. }
  55. int main(int argc, char const *argv[])
  56. {
  57.         char s[] = "AEAAAE";
  58.         //int l;
  59.         //l =strlen(s);
  60.         printf("%d\n", hash(s));
  61.         system("pause");
  62.         return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement