cs1713-day2-prog1a.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*-----------------------------------
  20. Name: Girish M
  21. Roll number: cs1713
  22. Date: 25 July 2017
  23. Program description:Write a program to find the number of (possibly overlapping)
  24. occurrences of the sequence B in A.
  25. Acknowledgements:
  26. ------------------------------------*/
  27. #include <stdio.h>
  28. int main()
  29. {
  30. int arr1[] = {1, 2, 2, 2, 2, 1, 2, 3, 3, 4, 3, 1, 2, 3, 1, 2, 3, 2, 1, 1};
  31. int arr2[] = {2, 2, 2};
  32. int i=0, j=0, k=0, match=0;
  33. while(j != 20)
  34. {
  35. if(arr1[k] == arr2[i])
  36. {
  37. i++;
  38. k++;
  39. }
  40. else
  41. {
  42. i=0;
  43. }
  44. if(i == 3)
  45. {
  46. match++;
  47. i=0;
  48. }
  49. k=j;
  50. j++;
  51. }
  52. printf("\nNo. of matches %d\n", match);
  53. return 0;
  54. }