| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn countScalar(comptime T: type, haystack: []const T, needle: T) usize { |
| 6 | var found: usize = 0; |
| 7 | for (haystack) |item| { |
| 8 | if (item == needle) { |
| 9 | found += 1; |
| 10 | } |
| 11 | } |
| 12 | return found; |
| 13 | } |
| 14 | |
| 15 | test { |
| 16 | try std.testing.expect(countScalar(u8, "The lazy brown fox jumped over the lazy dog.", ' ') == 8); |
| 17 | } |
| 18 | test { |
| 19 | try std.testing.expect(countScalar(u8, "kljsklksldajvaskidjklvadv", 'z') == 0); |
| 20 | } |