Advertisement
bkit4s0

[count num of character '' in string] use recursive finishe

Jan 1st, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAX 100
  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 countnumofAinstr(char s[],int n)
  35. {
  36.         int result;
  37.         char s1[MAX], s2[MAX];
  38.         if(strlen(s) == 1)
  39.         {
  40.             puts(s);
  41.             if(s[0]=='A')
  42.                 return 1;
  43.             else
  44.                 return 0;
  45.         }
  46.         split(s,s1,s2);
  47.         //result = countnumofAinstr(s1) + countnumofAinstr(s1);
  48.         return countnumofAinstr(s1,n/2) + countnumofAinstr(s2,n/2);
  49. }
  50. int main(int argc, char const *argv[])
  51. {
  52.         char s[] = "AAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAEEEEEAAAAAAAAAA";
  53.         int l;
  54.         l =strlen(s);
  55.         printf("%d\n", countnumofAinstr(s,l));
  56.         system("pause");
  57.         return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement