#convert character to string
1 messages · Page 1 of 1 (latest)
var str = "A".*;
while (str[0] <= 'Z') : (str[0] += 1) {
why not just
for (‘A’..’Z’+1) |c| {
const str = &.{c};
}
Hmm, I managed var str = [_]u8{'A'}; and then when I pass as string to a function, I must send the address &str (to pretend it's const?).
it is const
no you have to do it to make a slice
it's not
but you can just do const str: []const u8 = &.{‘A’}
one sec, I'magonna try all of your suggestions 🙂
if it’s &.{c}, it would be a []const u8
that’s what I meant
the summary is that you need to make some type of arrray and take the address of that, it will coerce to a slice
I couldn't compile, maybe I've misapplied...
for (‘G’..’I’+1) |cc| {
const jodi = &.{cc};
print("jodi score({s}) == {d}\n", .{ jodi, score(&jodi) });
}
I wonder if you can help me understand the relationship between address of (array or slice) vs const string
what is the function signature of score?
🙂 score iterates over the chars of the string returning a scrable score. 'a" = 1 point and 'z' = 10 points.
pub fn score(s: []const u8) u32 {
if score takes a []const u8, it should be called with score(jodi)
otherwise youre giving it a reference to a slice
(fwiw, jodi here isnt necessarily a []const u8, but it should be able to coerce for you)
usize doesn't coerce to u8
Are these equiv?
var str = [_]u8{'A'};
var str = "A".*;
it iterates over str.len which does not include the terminator