tpos.nim 538 B

12345678910111213141516171819202122232425262728293031323334
  1. discard """
  2. output: "6"
  3. """
  4. # test this particular function
  5. proc mypos(sub, s: string, start: int = 0): int =
  6. var
  7. i, j, M, N: int
  8. M = sub.len
  9. N = s.len
  10. i = start
  11. j = 0
  12. if i >= N:
  13. result = -1
  14. else:
  15. while true:
  16. if s[i] == sub[j]:
  17. inc(i)
  18. inc(j)
  19. else:
  20. i = i - j + 1
  21. j = 0
  22. if (j >= M) or (i >= N): break
  23. if j >= M:
  24. result = i - M
  25. else:
  26. result = -1
  27. var sub = "hello"
  28. var s = "world hello"
  29. write(stdout, mypos(sub, s))
  30. write stdout, "\n"
  31. #OUT 6