Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int find (char string[],char sub[]); //returns array position of first time substring is found
  7. int findFrom(char string[],char sub[],int n); //returns array position of first time substring is found STARTING FROM n index position of main string.
  8.  
  9. int main()
  10. {char string[200], sub[200], ch;
  11. int i, index = -5;
  12.  
  13. printf("Enter the main string \n");
  14. gets(string);
  15.  
  16. printf("Enter the substring: ");
  17. gets(sub);
  18.  
  19.  
  20. index = find(string, sub);
  21.  
  22. if (index < 0)
  23. printf("Substring not found");
  24.  
  25. else
  26. printf("Substring first found in %d position of the string",index+1);
  27. return 0;
  28.  
  29.  
  30.  
  31. }
  32.  
  33.  
  34. int find (char string[],char sub[])
  35. {int i, j = 0, k;
  36. char ch;
  37.  
  38. for (i = 0; string[i]!= 0; i++)
  39. {
  40. if (string[i] == sub[j])
  41. { k = i;
  42. while(sub[j]!=0)
  43. {
  44. if (string[k++] != sub[j++])
  45. break;
  46. }
  47.  
  48. if (sub[j] == 0)
  49. {
  50. return i; //a confirmed match has been found.
  51. }
  52. else
  53. {
  54. j = 0;
  55. }
  56.  
  57. }
  58.  
  59. }
  60.  
  61.  
  62. return -1;
  63.  
  64.  
  65. }
  66.  
  67. int findFrom(char string[],char sub[],int n)
  68. {
  69. int i, j = 0, k;
  70. char ch;
  71.  
  72. for (i = n; string[i]!= 0; i++)
  73. {
  74. if (string[i] == sub[j])
  75. { k = i;
  76. while(sub[j]!=0)
  77. {
  78. if (string[k++] != sub[j++])
  79. break;
  80. }
  81.  
  82. if (sub[j] == 0)
  83. {
  84. return i; //a confirmed match has been found.
  85. }
  86. else
  87. {
  88. j = 0;
  89. }
  90.  
  91. }
  92.  
  93. }
  94.  
  95.  
  96. return -1;
  97.  
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. //
  109. Second code
  110.  
  111.  
  112. #include <stdio.h>
  113. #include <stdlib.h>
  114. #include <string.h>
  115.  
  116. int findFrom(char string[],char sub[]); //returns array position of first time substring is found STARTING FROM n index position of main string.
  117.  
  118. int main()
  119. {char string[200], sub[200], ch;
  120. int i, index = -5, n = 0;
  121.  
  122. printf("Enter the main string \n");
  123. gets(string);
  124.  
  125. printf("Enter the substring: ");
  126. gets(sub);
  127.  
  128. for (i = 0; string[i]!= 0; i++)
  129. {
  130. index = findFrom(string + n,sub);
  131.  
  132. if (index > 0)
  133. {
  134. printf("%d ",index+n);
  135. i = index;
  136. n = index + n;
  137. }
  138.  
  139. index = -1;
  140. }
  141.  
  142.  
  143. if (n == 0)
  144. printf("String not found.");
  145.  
  146.  
  147.  
  148.  
  149. }
  150.  
  151.  
  152.  
  153.  
  154. int findFrom(char string[],char sub[])
  155. {
  156. int i, j = 0, k;
  157. char ch;
  158.  
  159. for (i = 0; string[i]!= 0; i++)
  160. {
  161. if (string[i] == sub[j])
  162. { k = i;
  163. while(sub[j]!=0)
  164. {
  165. if (string[k++] != sub[j++])
  166. break;
  167. }
  168.  
  169. if (sub[j] == 0)
  170. {
  171. return i; //a confirmed match has been found.
  172. }
  173. else
  174. {
  175. j = 0;
  176. }
  177.  
  178. }
  179.  
  180. }
  181.  
  182.  
  183. return -1;
  184.  
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement