Skip to content

Commit 53c5474

Browse files
Merge pull request #12 from DoctorLai/rot47
Add ROT47 Example
2 parents 56ff3de + c32a7f8 commit 53c5474

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This repository is intended as:
88
* A interview‑prep playground
99
* A reference for **concurrency, synchronization, low‑level and modern C++ techniques**
1010
* A growing set of **self‑contained, buildable examples**
11-
* A collection of more useful examples than leetcode puzzles.
11+
* A collection of more useful examples (and CLI tools) than leetcode puzzles.
1212

1313
Examples include (and will expand to):
1414

@@ -28,6 +28,8 @@ Examples include (and will expand to):
2828
* Performance‑oriented C++ idioms
2929
* Algorithms
3030
* [integer-factorization](./integer-factorization/)
31+
* Encoding
32+
* [rot47](./rot47/)
3133
* OOP
3234
* [virtual-interface](./virtual-interface/)
3335

rot47/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# pull in shared compiler settings
2+
include ../common.mk
3+
4+
# per-example flags
5+
# CXXFLAGS += -pthread
6+
7+
## get it from the folder name
8+
TARGET := $(notdir $(CURDIR))
9+
SRCS := $(wildcard *.cpp)
10+
OBJS := $(SRCS:.cpp=.o)
11+
12+
all: $(TARGET)
13+
14+
$(TARGET): $(OBJS)
15+
$(CXX) $(CXXFLAGS) -o $@ $^
16+
17+
%.o: %.cpp
18+
$(CXX) $(CXXFLAGS) -c $< -o $@
19+
20+
run: $(TARGET)
21+
./$(TARGET) $(ARGS)
22+
23+
clean:
24+
rm -f $(OBJS) $(TARGET)
25+
26+
# Delegates to top-level Makefile
27+
check-format:
28+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
29+
30+
.PHONY: all clean run check-format

rot47/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <string_view>
4+
#include <ranges>
5+
6+
[[nodiscard]]
7+
std::string
8+
rot47(std::string_view input)
9+
{
10+
auto transform = [](char c) { return (c >= 33 && c <= 126) ? static_cast<char>(33 + ((c - 33 + 47) % 94)) : c; };
11+
12+
std::string output;
13+
output.reserve(input.size());
14+
15+
for (char c : input | std::views::transform(transform)) {
16+
output.push_back(c);
17+
}
18+
19+
return output;
20+
}
21+
22+
int
23+
main(int argc, char* argv[])
24+
{
25+
for (int i = 1; i < argc; ++i) {
26+
std::cout << rot47(argv[i]);
27+
28+
if (i + 1 < argc) {
29+
std::cout << ' ';
30+
}
31+
}
32+
}

rot47/tests.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
./rot47
6+
7+
helloworld=$(./rot47 'w6==@ (@C=5P')
8+
9+
if [ "$helloworld" != "Hello World!" ]; then
10+
echo "Test failed: expected 'Hello World!' but got '$helloworld'"
11+
exit 1
12+
fi
-98.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)