fork(1) download
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int penalty_shoot(char* s){
  5. int count=0,i=0;
  6. while(s[i]!='\0'){
  7. if(s[i]=='2')
  8. if(s[i+1]=='1')
  9. count++;
  10. i++;
  11. }
  12. return count;
  13. }
  14.  
  15. int main() {
  16. int t;
  17. int i=0;
  18. scanf("%d ",&t); //t is for number of test cases.
  19. while(t--){
  20. char *str, c;
  21. str = (char*)malloc(1*sizeof(char));
  22. while(c = getc(stdin),c!='\n'&&c!=EOF)
  23. {
  24. str[i] = c;
  25. i++;
  26. str=realloc(str,(i+1)*sizeof(char));
  27. }
  28. str[i] ='\0';
  29. printf("%s\n",str);
  30. printf("%d\n",penalty_shoot(str));
  31.  
  32. free(str);
  33. str=NULL;
  34. i=0;
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 4480KB
stdin
3
101201212110
10101
2120
stdout
101201212110
2
10101
0
2120
1