Advertisement
Guest User

Untitled

a guest
May 30th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. char *strstr4(char *payload, char *token) {
  2. int found = 0;
  3. char *current = token;
  4.  
  5. while (*payload != '\0') {
  6. if (*payload == *current) {
  7. current++;
  8. if (*current == '\0') {
  9. found = 1;
  10. break;
  11. }
  12. } else {
  13. current = token;
  14. }
  15. payload++;
  16. }
  17.  
  18. if (!found)
  19. return NULL;
  20. else
  21. return payload;
  22. }
  23.  
  24. int indexOf(char *str, char *token) {
  25. int i = 0;
  26. int start = 0;
  27. char *ot = token;
  28. while (str[i] != '\0') {
  29. if (str[i] == *token) {
  30. if (strcmp(token, ot) == 0)
  31. start = i;
  32. token++;
  33. if (*token == '\0') {
  34. return start;
  35. }
  36. } else {
  37. token = ot;
  38. }
  39. i++;
  40. }
  41. return -1;
  42. }
  43.  
  44. int main() {
  45. char *s = "Hello World!";
  46. char *token = "Wor";
  47. char *res;
  48.  
  49. res = strstr4(s, token);
  50.  
  51. printf("%s\n", res);
  52.  
  53. res = strstr4(s, "za");
  54.  
  55. if (res == NULL) {
  56. printf("Not found\n");
  57. } else {
  58. printf("%s\n", res);
  59. }
  60.  
  61. res = strstr4(s, token);
  62. printf("s2: %s\n", res);
  63. res = strstr4(s, "H");
  64. printf("s2: %s\n", res);
  65.  
  66. printf("%d\n", indexOf("Hi", "i"));
  67. printf("%d\n", indexOf("Hi", "z"));
  68. printf("%d\n", indexOf("big powers | yolo string.zaajsd", "g.z"));
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement