str_increment / str_decrement

str_increment(string $string): string — str_decrement(string $string): string

PHP 8.3 alphanumeric increment/decrement: rightmost digit +1 with carry (z wraps to a, 9 wraps to 0); an all-carry prepends a same-family character (zz→aaa, ZZ→AAA, 99→100); no builtin elsewhere - hand-write it.

C C23

C23
/* No libc equivalent: hand-write the carry walk. Prepend character
   follows the FIRST char's family (digit -> '1', upper -> 'A', lower -> 'a'). */
#include <stdio.h>
#include <string.h>

static char after(char c) { /* incremented char, with wrap */
    if (c == '9') return '0';
    if (c == 'z') return 'a';
    if (c == 'Z') return 'A';
    return (char)(c + 1);
}

static void increment(const char *in, char *out) { /* out >= strlen(in)+1 */
    size_t n = strlen(in);
    strcpy(out, in);
    for (size_t i = n; i-- > 0;) {
        char nxt = after(out[i]);
        out[i] = nxt;
        if (nxt != '0' && nxt != 'a' && nxt != 'A') return; /* no carry */
    }
    /* all carried: prepend per the first char's family */
    char extra = (out[0] == '0') ? '1' : (out[0] == 'a') ? 'a' : 'A';
    memmove(out + 1, out, n + 1);
    out[0] = extra;
}

int main(void) {
    char out[16] = {0}; /* zero-initialized buffer */
    increment("abc", out); printf("%s\n", out);
    increment("Az", out); printf("%s\n", out);
    increment("zz", out); printf("%s\n", out);
    increment("y9", out); printf("%s\n", out);
    increment("99", out); printf("%s\n", out);
    return 0;
}
Output
abd
Ba
aaa
z0
100

C++ C++26

C++26
// Hand-written carry walk over std::string; prepend follows the first
// char's family (digit -> '1', upper -> 'A', lower -> 'a').
#include <iostream>
#include <string>

static char after(char c) { /* incremented char, with wrap */
    if (c == '9') return '0';
    if (c == 'z') return 'a';
    if (c == 'Z') return 'A';
    return (char)(c + 1);
}

static std::string increment(std::string s) {
    for (size_t i = s.size(); i-- > 0;) {
        char nxt = after(s[i]);
        s[i] = nxt;
        if (nxt != '0' && nxt != 'a' && nxt != 'A') return s; // no carry
    }
    char extra = (s[0] == '0') ? '1' : (s[0] == 'a') ? 'a' : 'A';
    return extra + s; // all carried: prepend
}

int main() {
    std::cout << increment("abc") << "\n";
    std::cout << increment("Az") << "\n";
    std::cout << increment("zz") << "\n";
    std::cout << increment("y9") << "\n";
    std::cout << increment("99") << "\n";
}
Output
abd
Ba
aaa
z0
100

C# v11.0.100

v11.0.100
// Hand-written carry walk; prepend follows the first char's family.
static string Increment(string s) {
    var b = s.ToCharArray();
    for (int i = b.Length - 1; i >= 0; --i) {
        char nxt = b[i] switch { '9' => '0', 'z' => 'a', 'Z' => 'A', _ => (char)(b[i] + 1) };
        b[i] = nxt;
        if (nxt is not ('0' or 'a' or 'A')) return new string(b); // no carry
    }
    char extra = b[0] switch { '0' => '1', 'a' => 'a', _ => 'A' };
    return extra + new string(b); // all carried: prepend
}

Console.WriteLine(Increment("abc"));
Console.WriteLine(Increment("Az"));
Console.WriteLine(Increment("zz"));
Console.WriteLine(Increment("y9"));
Console.WriteLine(Increment("99"));
Output
abd
Ba
aaa
z0
100

Go v1.27.4

v1.27.4
// Hand-written carry walk; prepend follows the first char's family.
package main

import "fmt"

func after(c byte) byte { // incremented char, with wrap
	switch c {
	case '9':
		return '0'
	case 'z':
		return 'a'
	case 'Z':
		return 'A'
	}
	return c + 1
}

func increment(s string) string {
	b := []byte(s)
	for i := len(b) - 1; i >= 0; i-- {
		nxt := after(b[i])
		b[i] = nxt
		if nxt != '0' && nxt != 'a' && nxt != 'A' {
			return string(b) // no carry
		}
	}
	extra := byte('A') // all carried: prepend per family
	if b[0] == '0' {
		extra = '1'
	} else if b[0] == 'a' {
		extra = 'a'
	}
	return string(append([]byte{extra}, b...))
}

func main() {
	fmt.Println(increment("abc"))
	fmt.Println(increment("Az"))
	fmt.Println(increment("zz"))
	fmt.Println(increment("y9"))
	fmt.Println(increment("99"))
}
Output
abd
Ba
aaa
z0
100

Java v27.0.1

v27.0.1
// Hand-written carry walk; prepend follows the first char's family.
public class Main {
    static char after(char c) { // incremented char, with wrap
        if (c == '9') return '0';
        if (c == 'z') return 'a';
        if (c == 'Z') return 'A';
        return (char) (c + 1);
    }
    static String increment(String s) {
        char[] b = s.toCharArray();
        for (int i = b.length - 1; i >= 0; --i) {
            char nxt = after(b[i]);
            b[i] = nxt;
            if (nxt != '0' && nxt != 'a' && nxt != 'A') return new String(b); // no carry
        }
        char extra = b[0] == '0' ? '1' : b[0] == 'a' ? 'a' : 'A';
        return extra + new String(b); // all carried: prepend
    }
    public static void main(String[] args) {
        System.out.println(increment("abc"));
        System.out.println(increment("Az"));
        System.out.println(increment("zz"));
        System.out.println(increment("y9"));
        System.out.println(increment("99"));
    }
}
Output
abd
Ba
aaa
z0
100

JavaScript v26.13.1

v26.13.1
// Hand-written carry walk; prepend follows the first char's family.
function increment(s) { // str_increment twin
    const b = [...s];
    for (let i = b.length - 1; i >= 0; --i) {
        const nxt = b[i] === "9" ? "0" : b[i] === "z" ? "a" : b[i] === "Z" ? "A" : String.fromCharCode(b[i].charCodeAt(0) + 1);
        b[i] = nxt;
        if (nxt !== "0" && nxt !== "a" && nxt !== "A") return b.join(""); // no carry
    }
    const extra = b[0] === "0" ? "1" : b[0] === "a" ? "a" : "A";
    return extra + b.join(""); // all carried: prepend
}
console.log(increment("abc"));
console.log(increment("Az"));
console.log(increment("zz"));
console.log(increment("y9"));
console.log(increment("99"));
Output
abd
Ba
aaa
z0
100

PHP v8.6.0

v8.6.0
// str_increment / str_decrement (PHP 8.3): alphanumeric ASCII ++/-- with
// carry: 'z' wraps to 'a' and carries, '9' wraps to '0'; an all-carry
// prepends a digit of the same family ('zz'->'zaa', 'ZZ'->'AAA', '99'->'100').
echo str_increment("abc"), ",", str_increment("Az"), ",", str_increment("zz"), ",", str_increment("y9"), ",", str_increment("99"), "\n";
echo str_decrement("abd"), ",", str_decrement("Ba"), ",", str_decrement("Az"), "\n";
var_dump(str_increment(str_decrement("b")) === "b"); // round trip
Output
abd,Ba,aaa,z0,100
abc,Az,Ay
bool(true)

Python v3.15.2

v3.15.2
# Hand-written carry walk; prepend follows the first char's family.
def str_increment(s): # PHP 8.3 twin
    chars = list(s)
    for i in range(len(chars) - 1, -1, -1):
        c = chars[i]
        nxt = "0" if c == "9" else "a" if c == "z" else "A" if c == "Z" else chr(ord(c) + 1)
        chars[i] = nxt
        if nxt not in ("0", "a", "A"):
            return "".join(chars) # no carry
    extra = "1" if chars[0] == "0" else "a" if chars[0] == "a" else "A"
    return extra + "".join(chars) # all carried: prepend

print(str_increment("abc"))
print(str_increment("Az"))
print(str_increment("zz"))
print(str_increment("y9"))
print(str_increment("99"))
Output
abd
Ba
aaa
z0
100

Rust v1.99.1

v1.99.1
// Hand-written carry walk; prepend follows the first char's family.
fn after(c: char) -> char { // incremented char, with wrap
    match c {
        '9' => '0',
        'z' => 'a',
        'Z' => 'A',
        other => ((other as u8) + 1) as char,
    }
}

fn increment(s: &str) -> String {
    let mut b: Vec<char> = s.chars().collect();
    for i in (0..b.len()).rev() {
        let nxt = after(b[i]);
        b[i] = nxt;
        if nxt != '0' && nxt != 'a' && nxt != 'A' {
            return b.into_iter().collect(); // no carry
        }
    }
    let extra = match b[0] {
        '0' => '1',
        'a' => 'a',
        _ => 'A',
    };
    extra.to_string() + &b.into_iter().collect::<String>() // all carried: prepend
}

fn main() {
    println!("{}", increment("abc"));
    println!("{}", increment("Az"));
    println!("{}", increment("zz"));
    println!("{}", increment("y9"));
    println!("{}", increment("99"));
}
Output
abd
Ba
aaa
z0
100

TypeScript v26.13.1

v26.13.1
// Same hand-written carry walk as JavaScript.
function increment(s: string): string {
    const b: string[] = [...s];
    for (let i = b.length - 1; i >= 0; --i) {
        const nxt: string = b[i] === "9" ? "0" : b[i] === "z" ? "a" : b[i] === "Z" ? "A" : String.fromCharCode(b[i].charCodeAt(0) + 1);
        b[i] = nxt;
        if (nxt !== "0" && nxt !== "a" && nxt !== "A") return b.join("");
    }
    const extra: string = b[0] === "0" ? "1" : b[0] === "a" ? "a" : "A";
    return extra + b.join("");
}
console.log(increment("abc"));
console.log(increment("Az"));
console.log(increment("zz"));
console.log(increment("y9"));
console.log(increment("99"));
Output
abd
Ba
aaa
z0
100

Language differences

New in PHP 8.3 - Perl-style string increment, the functional form of "az"++: carry rules wrap z to a, Z to A, 9 to 0, intermediate positions +1; an all-carry prepends a same-family character - verified on PHP 8.6: zz→aaa and 99→100 (the manual’s ZZ→AAA is the same shape); the task list’s zz→zaa was a misjudgment - the verified runtime output wins; str_decrement is the reverse borrow (a borrows to z, A to Z, 0 to 9), and the manual’s AA→Z shows the all-borrow edge case where the string SHRINKS; error branches: an empty string or a non-alphanumeric trailing character throws ValueError; contrast with the ++ operator: PHP’s $s++ carries legacy behavior for null and numeric strings while $s-- is a no-op - the two 8.3 functions close the semantics into something explicit, symmetric, and testable. Other languages: no builtin anywhere (Java/C#/Python/Go/JS/Rust/C/C++ all hand-write; the entries share one carry algorithm - walk from the right, keep borrowing on wrap, prepend per the first char’s family of 1/A/a); the hand-written Python aligns verbatim with the PHP output. Teaching anchor: the carry-notation "next identifier" algorithm (Excel column names A,B,...,Z,AA are the base-26 special case); the two classic bugs are forgetting to KEEP carrying after a wrap, and picking the wrong prepend family when everything carries.