Zig: completed Reverse String

This commit is contained in:
Andrew Scott 2024-09-20 17:07:22 -04:00
parent 1e8c06ae40
commit fe6da9630c
Signed by: a
GPG key ID: 7CD5A5977E4931C1
6 changed files with 145 additions and 0 deletions

View file

@ -0,0 +1,19 @@
{
"authors": [
"ee7"
],
"files": {
"solution": [
"reverse_string.zig"
],
"test": [
"test_reverse_string.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Reverse a given string.",
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"reverse-string","id":"506c2a4923c9458e9a24cda8f948de0b","url":"https://exercism.org/tracks/zig/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View file

@ -0,0 +1,53 @@
# Help
## Running the tests
Write your code in `<exercise_name>.zig`.
To run the tests for an exercise, run:
```bash
zig test test_exercise_name.zig
```
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
## Submitting your solution
You can submit your solution using the `exercism submit reverse_string.zig` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Zig track's documentation](https://exercism.org/docs/tracks/zig)
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
- [Ziglings][ziglings] is highly recommended.
Learn Zig by fixing tiny broken programs.
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
- [/r/Zig][reddit] is the main Zig subreddit.
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
[discord]: https://discordapp.com
[discord-zig]: https://discord.com/invite/gxsFFjE
[documentation]: https://ziglang.org/documentation/master
[irc]: https://webchat.freenode.net/?channels=%23zig
[reddit]: https://www.reddit.com/r/Zig
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
[zig-guide]: https://zig.guide/
[ziglings]: https://codeberg.org/ziglings/exercises

View file

@ -0,0 +1,30 @@
# Reverse String
Welcome to Reverse String on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Introduction
Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming.
For example, in bioinformatics, reversing the sequence of DNA or RNA strings is often important for various analyses, such as finding complementary strands or identifying palindromic sequences that have biological significance.
## Instructions
Your task is to reverse a given string.
Some examples:
- Turn `"stressed"` into `"desserts"`.
- Turn `"strops"` into `"sports"`.
- Turn `"racecar"` into `"racecar"`.
## Source
### Created by
- @ee7
### Based on
Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

View file

@ -0,0 +1,8 @@
const std = @import("std");
/// Writes a reversed copy of `s` to `buffer`.
pub fn reverse(buffer: []u8, s: []const u8) []u8 {
@memcpy(buffer, s);
std.mem.reverse(u8, buffer);
return buffer;
}

View file

@ -0,0 +1,34 @@
const std = @import("std");
const testing = std.testing;
const reverse_string = @import("reverse_string.zig");
fn testReverse(comptime s: []const u8, expected: []const u8) !void {
var buffer: [s.len]u8 = undefined;
const actual = reverse_string.reverse(&buffer, s);
try testing.expectEqualStrings(expected, actual);
}
test "an empty string" {
try testReverse("", "");
}
test "a word" {
try testReverse("robot", "tobor");
}
test "a capitalized word" {
try testReverse("Ramen", "nemaR");
}
test "a sentence with punctuation" {
try testReverse("I'm hungry!", "!yrgnuh m'I");
}
test "a palindrome" {
try testReverse("racecar", "racecar");
}
test "an even-sized word" {
try testReverse("drawer", "reward");
}