Advertisement
bkit4s0

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

Dec 31st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #icclude<stdio.h>
  2. #include<string.h>
  3. void split(char *s, char *s1, char *s2)
  4. {
  5.     int l, m;
  6.     l = strlen(s);
  7.     m = l/2;
  8.     for (int i = 0; i < m; ++i)
  9.     {
  10.         s1[i] = s[i];
  11.         s2[i] = s[l - m + i];
  12.     }
  13. }
  14. int numofA(char *s)
  15. {
  16.     int count;
  17.     count = 0;
  18.     for (int i = 0; i < strlen(s); ++i)
  19.     {
  20.         if(s[i] == 'A')
  21.             count++;
  22.     }
  23.     return count;
  24. }
  25. int countnumofAinstr(char *s)
  26. {
  27.     int result;
  28.     char *s1, *s2;
  29.     result = numofA(s);
  30.     if(strlen(s) > 1)
  31.     {
  32.         split(s,s1,s2);
  33.         result = countnumofAinstr(s1) + countnumofAinstr(s1);
  34.     }
  35.     return result;
  36. }
  37. int main(int argc, char const *argv[])
  38. {
  39.     char *s = "AEEAEEAAE";
  40.     printf("%d\n", countnumofAinstr(s));
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement