ucwords

ucwords(string $string, string $separators = " \t\r\n\f\v"): string

Uppercases the first character of each word. The default word boundaries are only the six whitespace characters (space/tab/CR/LF/FF/VT) - apostrophes and hyphens do not count. Python's title() and C#'s ToTitleCase follow different rules; see the comparison.

C C23

C23
#include <ctype.h>
#include <stdio.h>

int main(void) {
    const char *s = "hello world";
    int word_start = 1; // the very first character opens a word
    for (const char *p = s; *p != '\0'; ++p) {
        if (*p == ' ') { // separator: the next character starts a new word
            word_start = 1;
            putchar(*p);
        } else {
            putchar(word_start ? toupper((unsigned char)*p) : *p);
            word_start = 0;
        }
    }
    putchar('\n');
    return 0;
}
Output
Hello World

C++ C++26

C++26
#include <cctype>
#include <iostream>

int main() {
    const char *s = "hello world";
    bool word_start = true; // start of string opens a word
    for (const char *p = s; *p != '\0'; ++p) {
        if (*p == ' ') { // separator: the next character starts a new word
            word_start = true;
            std::cout << *p;
        } else {
            std::cout << (word_start ? (char)toupper((unsigned char)*p) : *p);
            word_start = false;
        }
    }
    std::cout << "\n";
    return 0;
}
Output
Hello World

C# v11.0.100

v11.0.100
// TextInfo.ToTitleCase is the closest built-in (culture-aware — pinned to InvariantCulture here)
var ti = System.Globalization.CultureInfo.InvariantCulture.TextInfo;
Console.WriteLine(ti.ToTitleCase("hello world")); // Hello World
Console.WriteLine(ti.ToTitleCase("mike o'hara")); // Mike O'hara: ' is not a separator
Console.WriteLine(ti.ToTitleCase("hello-world")); // Hello-World: '-' IS a separator here
Output
Hello World
Mike O'hara
Hello-World

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
	"unicode"
)

// no built-in: strings.Title was deprecated in Go 1.18 — hand-written state machine
func ucwords(s string) string {
	word_start := true
	return strings.Map(func(r rune) rune {
		if unicode.IsSpace(r) {
			word_start = true
			return r
		}
		if word_start {
			word_start = false
			return unicode.ToUpper(r)
		}
		return r
	}, s)
}

func main() {
	fmt.Println(ucwords("hello world")) // Hello World
}
Output
Hello World

Java v27.0.1

v27.0.1
public class Main {
    // no built-in title case — hand-written state machine
    // (Apache commons-lang WordUtils is the classic third-party option)
    static String ucwords(String s) {
        StringBuilder sb = new StringBuilder(s);
        boolean word_start = true;
        for (int i = 0; i < sb.length(); i++) {
            char c = sb.charAt(i);
            if (Character.isWhitespace(c)) {
                word_start = true;
            } else {
                if (word_start) sb.setCharAt(i, Character.toUpperCase(c));
                word_start = false;
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(ucwords("hello world")); // Hello World
    }
}
Output
Hello World

JavaScript v26.13.1

v26.13.1
// no built-in title case — hand-written state machine
function ucwords(s) {
  let out = ""
  let word_start = true
  for (const c of s) {
    if (c === " ") {
      word_start = true
      out += c
    } else {
      out += word_start ? c.toUpperCase() : c
      word_start = false
    }
  }
  return out
}
console.log(ucwords("hello world")) // Hello World
Output
Hello World

PHP v8.6.0

v8.6.0
echo ucwords("hello world"), "\n";      // Hello World
echo ucwords("mike o'hara"), "\n";      // Mike O'hara: ' is not a word separator
echo ucwords("hello-world"), "\n";      // Hello-world: neither is '-'
echo ucwords("hello-world", "-"), "\n"; // Hello-World: custom separators argument
Output
Hello World
Mike O'hara
Hello-world
Hello-World

Python v3.15.2

v3.15.2
print("hello world".title())  # Hello World
# title() treats apostrophes and hyphens as word boundaries — a known difference from PHP:
print("mike o'hara".title())   # Mike O'Hara
print("hello-world".title())   # Hello-World
Output
Hello World
Mike O'Hara
Hello-World

Rust v1.99.1

v1.99.1
// no built-in title case — hand-written state machine
fn ucwords(s: &str) -> String {
    let mut word_start = true;
    s.chars()
        .map(|c| {
            if c.is_whitespace() {
                word_start = true;
                c
            } else if word_start {
                word_start = false;
                c.to_uppercase().next().unwrap()
            } else {
                c
            }
        })
        .collect()
}

fn main() {
    println!("{}", ucwords("hello world")); // Hello World
}
Output
Hello World

TypeScript v26.13.1

v26.13.1
// no built-in title case — hand-written state machine
function ucwords(s: string): string {
  let out = "";
  let word_start = true;
  for (const c of s) {
    if (c === " ") {
      word_start = true;
      out += c;
    } else {
      out += word_start ? c.toUpperCase() : c;
      word_start = false;
    }
  }
  return out;
}
console.log(ucwords("hello world")); // Hello World
Output
Hello World

Language differences

C: no built-in; hand-written toupper state machine - the argument must be cast to unsigned char (a negative index is undefined behavior).

C++: no built-in; same state machine as C.

C#: TextInfo.ToTitleCase is the closest built-in (culture-aware - the example pins InvariantCulture); measured: no uppercase after an apostrophe (mike o'hara -> Mike O'hara) but uppercase after a hyphen (hello-world -> Hello-World).

Go: no built-in; strings.Title was deprecated in Go 1.18 - hand-write the state machine.

Java: no built-in; Apache commons-lang WordUtils is the classic third-party option.

JavaScript: no built-in; hand-write a state machine or a \b\w regex.

PHP: the default separators are only six whitespace chars; apostrophes and hyphens are NOT word boundaries (mike o'hara -> Mike O'hara) - pass the second argument to customize (e.g. ucwords($s, " '-")); since 8.2 only ASCII letters are converted and setlocale no longer matters; for multibyte text use mb_convert_case with MB_CASE_TITLE.

Python: title() treats apostrophes AND hyphens as word boundaries (mike o'hara -> Mike O'Hara) - a behavioral difference from PHP in the opposite direction of C#.

Rust: no built-in; char::to_uppercase may expand to multiple chars (e.g. ß -> SS) - the example takes the first.

TypeScript: same as JavaScript.