1

Given a character (u8) in zig, how can I make sure it is in ASCII uppercase?

Does the standard library provide helper functions for this?

1 Answer 1

3

Use std.ascii.toUpper(c)

In general, the standard library provides the following functions:

And the corresponding functions for lowercase letters:

Here is some example usage:

const std = @import("std")
const expect = std.testing.expect;

const upper_a: u8 = 'A';
const lower_a: u8 = 'a';

const mixed_str: []const u8 = "AbCd";
var out_str = [_]u8{'0', '0', '0', '0'};

try expect(std.ascii.isUpper(upper_a));
try expect(std.ascii.toUpper(lower_a) == upper_a);
try expect(std.mem.eql(u8, std.ascii.upperString(&out_str, mixed_str), "ABCD"));
try expect(std.mem.eql(u8, &out_str, "ABCD")); // upperString writes to out_str

try expect(std.ascii.isLower(lower_a));
try expect(std.ascii.toLower(upper_a) == lower_a);
try expect(std.mem.eql(u8, std.ascii.lowerString(&out_str, mixed_str), "abcd"));
try expect(std.mem.eql(u8, &out_str, "abcd")); // lowerString writes to out_str
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.