tpos.nim 540 B

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