Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void clean_buffer()
  6. {
  7. int c;
  8. while((c = getchar()) != '\n');
  9. }
  10.  
  11. int substr(char* str1, char* str2, int** result);
  12.  
  13. int main()
  14. {
  15. int str1_len;
  16. int str2_len;
  17. scanf("%i %i", &str1_len, &str2_len);
  18. clean_buffer();
  19.  
  20. char* str1;
  21. char* str2;
  22.  
  23. str1=malloc((str1_len+1)*sizeof(char));
  24. str2=malloc((str2_len+1)*sizeof(char));
  25. for(int i=0;i<str1_len;i++){
  26. scanf("%c",&str1[i]);
  27. }
  28. clean_buffer();
  29. str1[str1_len]='\0';
  30. for(int i=0;i<str2_len;i++){
  31. scanf("%c",&str2[i]);
  32. }
  33. str2[str2_len]='\0';
  34. int* result;
  35. int arr_len = substr(str1, str2, &result);
  36. int i = 0;
  37. for(i = 0; i < arr_len; i++)
  38. printf("%i ", result[i]);
  39. free(result);
  40. free(str1);
  41. free(str2);
  42. }
  43.  
  44. int substr(char* str1, char* str2, int** result)
  45. {
  46. int maxsize=strlen(str2);
  47. int count=-1;
  48. int* listof = malloc(maxsize*sizeof(int));
  49. for(int i=0;i<strlen(str2);i++){
  50. if(str2[i]==str1[0]){
  51. int ch=1;
  52. for(int j=0;j<strlen(str1);j++){
  53. if(str2[i+j]!=str1[j]){
  54. ch=0;
  55. break;
  56. }
  57. }
  58. if(ch==0){
  59. continue;
  60. }else{
  61. count++;
  62. listof[count]=i;
  63. }
  64. }
  65. }
  66. *result=listof;
  67. return count+1;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement