[{"data":1,"prerenderedAt":587},["ShallowReactive",2],{"guide-string-reversal-algorithms":3},{"id":4,"title":5,"body":6,"date":578,"description":579,"extension":580,"meta":581,"navigation":582,"path":583,"readingTime":65,"seo":584,"stem":585,"__hash__":586},"guides\u002Fguides\u002Fstring-reversal-algorithms.md","String Reversal Algorithms Compared",{"type":7,"value":8,"toc":569},"minimark",[9,13,18,21,81,146,149,153,156,181,184,228,231,235,238,253,313,340,357,360,364,367,455,458,462,465,473,476,496,499,529,533,552,556,565],[10,11,12],"p",{},"Reversing a string is one of the most common programming exercises — and one of the most deceptively complex. While the basic algorithm is straightforward, the optimal approach varies by language, string encoding, and whether you need in-place mutation. This guide compares the major algorithms and their trade-offs.",[14,15,17],"h2",{"id":16},"iterative-reversal","Iterative Reversal",[10,19,20],{},"The iterative approach swaps characters from both ends, moving toward the center. It runs in O(n) time with O(1) extra space (for mutable strings):",[22,23,28],"pre",{"className":24,"code":25,"language":26,"meta":27,"style":27},"language-python shiki shiki-themes github-light github-dark","def reverse_iterative(s):\n    chars = list(s)\n    left, right = 0, len(chars) - 1\n    while left \u003C right:\n        chars[left], chars[right] = chars[right], chars[left]\n        left += 1\n        right -= 1\n    return ''.join(chars)\n","python","",[29,30,31,39,45,51,57,63,69,75],"code",{"__ignoreMap":27},[32,33,36],"span",{"class":34,"line":35},"line",1,[32,37,38],{},"def reverse_iterative(s):\n",[32,40,42],{"class":34,"line":41},2,[32,43,44],{},"    chars = list(s)\n",[32,46,48],{"class":34,"line":47},3,[32,49,50],{},"    left, right = 0, len(chars) - 1\n",[32,52,54],{"class":34,"line":53},4,[32,55,56],{},"    while left \u003C right:\n",[32,58,60],{"class":34,"line":59},5,[32,61,62],{},"        chars[left], chars[right] = chars[right], chars[left]\n",[32,64,66],{"class":34,"line":65},6,[32,67,68],{},"        left += 1\n",[32,70,72],{"class":34,"line":71},7,[32,73,74],{},"        right -= 1\n",[32,76,78],{"class":34,"line":77},8,[32,79,80],{},"    return ''.join(chars)\n",[22,82,86],{"className":83,"code":84,"language":85,"meta":27,"style":27},"language-c shiki shiki-themes github-light github-dark","\u002F\u002F In-place reversal of a null-terminated C string\nvoid reverse(char *s) {\n    int left = 0, right = strlen(s) - 1;\n    while (left \u003C right) {\n        char tmp = s[left];\n        s[left] = s[right];\n        s[right] = tmp;\n        left++;\n        right--;\n    }\n}\n","c",[29,87,88,93,98,103,108,113,118,123,128,134,140],{"__ignoreMap":27},[32,89,90],{"class":34,"line":35},[32,91,92],{},"\u002F\u002F In-place reversal of a null-terminated C string\n",[32,94,95],{"class":34,"line":41},[32,96,97],{},"void reverse(char *s) {\n",[32,99,100],{"class":34,"line":47},[32,101,102],{},"    int left = 0, right = strlen(s) - 1;\n",[32,104,105],{"class":34,"line":53},[32,106,107],{},"    while (left \u003C right) {\n",[32,109,110],{"class":34,"line":59},[32,111,112],{},"        char tmp = s[left];\n",[32,114,115],{"class":34,"line":65},[32,116,117],{},"        s[left] = s[right];\n",[32,119,120],{"class":34,"line":71},[32,121,122],{},"        s[right] = tmp;\n",[32,124,125],{"class":34,"line":77},[32,126,127],{},"        left++;\n",[32,129,131],{"class":34,"line":130},9,[32,132,133],{},"        right--;\n",[32,135,137],{"class":34,"line":136},10,[32,138,139],{},"    }\n",[32,141,143],{"class":34,"line":142},11,[32,144,145],{},"}\n",[10,147,148],{},"The iterative method is the most efficient general-purpose algorithm. It makes exactly ⌊n\u002F2⌋ swaps and uses constant additional memory. For languages where strings are immutable (Python, Java, JavaScript), you pay O(n) to create the character array, but the swap logic itself is optimal.",[14,150,152],{"id":151},"recursive-reversal","Recursive Reversal",[10,154,155],{},"A recursive implementation reverses the outer characters and recurses on the inner substring:",[22,157,159],{"className":24,"code":158,"language":26,"meta":27,"style":27},"def reverse_recursive(s):\n    if len(s) \u003C= 1:\n        return s\n    return reverse_recursive(s[1:]) + s[0]\n",[29,160,161,166,171,176],{"__ignoreMap":27},[32,162,163],{"class":34,"line":35},[32,164,165],{},"def reverse_recursive(s):\n",[32,167,168],{"class":34,"line":41},[32,169,170],{},"    if len(s) \u003C= 1:\n",[32,172,173],{"class":34,"line":47},[32,174,175],{},"        return s\n",[32,177,178],{"class":34,"line":53},[32,179,180],{},"    return reverse_recursive(s[1:]) + s[0]\n",[10,182,183],{},"While elegant, this approach has serious practical drawbacks:",[185,186,187,200],"table",{},[188,189,190],"thead",{},[191,192,193,197],"tr",{},[194,195,196],"th",{},"Issue",[194,198,199],{},"Impact",[201,202,203,212,220],"tbody",{},[191,204,205,209],{},[206,207,208],"td",{},"Stack depth",[206,210,211],{},"A string of length n requires n stack frames; long strings cause stack overflow",[191,213,214,217],{},[206,215,216],{},"Time complexity",[206,218,219],{},"Each concatenation creates a new string — O(n²) total in languages with immutable strings",[191,221,222,225],{},[206,223,224],{},"Memory",[206,226,227],{},"O(n) stack space plus O(n²) intermediate allocations",[10,229,230],{},"Tail-call optimization can mitigate the stack issue in some languages (Scheme, Erlang), but most mainstream languages — including Python, JavaScript, and Java — do not guarantee TCO. Recursive reversal is best treated as an academic exercise rather than a production technique.",[14,232,234],{"id":233},"built-in-and-functional-approaches","Built-In and Functional Approaches",[10,236,237],{},"Most modern languages provide a built-in reverse or allow a concise functional expression:",[22,239,241],{"className":24,"code":240,"language":26,"meta":27,"style":27},"# Python\nreversed_s = s[::-1]\n",[29,242,243,248],{"__ignoreMap":27},[32,244,245],{"class":34,"line":35},[32,246,247],{},"# Python\n",[32,249,250],{"class":34,"line":41},[32,251,252],{},"reversed_s = s[::-1]\n",[22,254,258],{"className":255,"code":256,"language":257,"meta":27,"style":27},"language-javascript shiki shiki-themes github-light github-dark","\u002F\u002F JavaScript\nconst reversed = s.split('').reverse().join('');\n","javascript",[29,259,260,266],{"__ignoreMap":27},[32,261,262],{"class":34,"line":35},[32,263,265],{"class":264},"sJ8bj","\u002F\u002F JavaScript\n",[32,267,268,272,276,279,283,287,290,294,297,300,303,306,308,310],{"class":34,"line":41},[32,269,271],{"class":270},"szBVR","const",[32,273,275],{"class":274},"sj4cs"," reversed",[32,277,278],{"class":270}," =",[32,280,282],{"class":281},"sVt8B"," s.",[32,284,286],{"class":285},"sScJk","split",[32,288,289],{"class":281},"(",[32,291,293],{"class":292},"sZZnC","''",[32,295,296],{"class":281},").",[32,298,299],{"class":285},"reverse",[32,301,302],{"class":281},"().",[32,304,305],{"class":285},"join",[32,307,289],{"class":281},[32,309,293],{"class":292},[32,311,312],{"class":281},");\n",[22,314,318],{"className":315,"code":316,"language":317,"meta":27,"style":27},"language-rust shiki shiki-themes github-light github-dark","\u002F\u002F Rust — reverses in place on a mutable vector of chars\nlet mut chars: Vec\u003Cchar> = s.chars().collect();\nchars.reverse();\nlet reversed: String = chars.into_iter().collect();\n","rust",[29,319,320,325,330,335],{"__ignoreMap":27},[32,321,322],{"class":34,"line":35},[32,323,324],{},"\u002F\u002F Rust — reverses in place on a mutable vector of chars\n",[32,326,327],{"class":34,"line":41},[32,328,329],{},"let mut chars: Vec\u003Cchar> = s.chars().collect();\n",[32,331,332],{"class":34,"line":47},[32,333,334],{},"chars.reverse();\n",[32,336,337],{"class":34,"line":53},[32,338,339],{},"let reversed: String = chars.into_iter().collect();\n",[22,341,345],{"className":342,"code":343,"language":344,"meta":27,"style":27},"language-haskell shiki shiki-themes github-light github-dark","-- Haskell\nreverseString = reverse\n","haskell",[29,346,347,352],{"__ignoreMap":27},[32,348,349],{"class":34,"line":35},[32,350,351],{},"-- Haskell\n",[32,353,354],{"class":34,"line":41},[32,355,356],{},"reverseString = reverse\n",[10,358,359],{},"Built-in functions delegate to optimized native code, typically using the same two-pointer swap internally. They should be your first choice in production code — they are faster, tested, and more readable than hand-rolled loops.",[14,361,363],{"id":362},"in-place-vs-copy-reversal","In-Place vs. Copy Reversal",[10,365,366],{},"Whether you can reverse a string in place depends on the language's string representation:",[185,368,369,382],{},[188,370,371],{},[191,372,373,376,379],{},[194,374,375],{},"Language",[194,377,378],{},"Mutable?",[194,380,381],{},"In-Place Possible?",[201,383,384,395,408,426,437,446],{},[191,385,386,389,392],{},[206,387,388],{},"C",[206,390,391],{},"Yes (char array)",[206,393,394],{},"Yes",[191,396,397,400,406],{},[206,398,399],{},"C++",[206,401,402,405],{},[29,403,404],{},"std::string"," is mutable",[206,407,394],{},[191,409,410,413,420],{},[206,411,412],{},"Rust",[206,414,415,416,419],{},"No (owned ",[29,417,418],{},"String",")",[206,421,422,423],{},"Via ",[29,424,425],{},"Vec\u003Cchar>",[191,427,428,431,434],{},[206,429,430],{},"Python",[206,432,433],{},"No",[206,435,436],{},"Must create new string",[191,438,439,442,444],{},[206,440,441],{},"Java",[206,443,433],{},[206,445,436],{},[191,447,448,451,453],{},[206,449,450],{},"JavaScript",[206,452,433],{},[206,454,436],{},[10,456,457],{},"Attempting in-place reversal on an immutable string forces you to convert it to a mutable structure first, which itself costs O(n) space. In these languages, the space advantage of iterative reversal disappears, and built-in methods become the clear winner.",[14,459,461],{"id":460},"reversing-words-vs-characters","Reversing Words vs. Characters",[10,463,464],{},"A related problem is reversing the order of words while preserving each word's characters:",[22,466,471],{"className":467,"code":469,"language":470},[468],"language-text","Input:  \"the sky is blue\"\nOutput: \"blue is sky the\"\n","text",[29,472,469],{"__ignoreMap":27},[10,474,475],{},"The standard approach: reverse the entire string, then reverse each word individually.",[22,477,479],{"className":24,"code":478,"language":26,"meta":27,"style":27},"def reverse_words(s):\n    words = s.split()\n    return ' '.join(reversed(words))\n",[29,480,481,486,491],{"__ignoreMap":27},[32,482,483],{"class":34,"line":35},[32,484,485],{},"def reverse_words(s):\n",[32,487,488],{"class":34,"line":41},[32,489,490],{},"    words = s.split()\n",[32,492,493],{"class":34,"line":47},[32,494,495],{},"    return ' '.join(reversed(words))\n",[10,497,498],{},"For in-place C-style solutions, the two-step reverse avoids allocating extra memory:",[22,500,502],{"className":83,"code":501,"language":85,"meta":27,"style":27},"\u002F\u002F Step 1: reverse entire string\nreverse(entire_string);\n\u002F\u002F Step 2: reverse each word individually\nfor each word in string:\n    reverse(word);\n",[29,503,504,509,514,519,524],{"__ignoreMap":27},[32,505,506],{"class":34,"line":35},[32,507,508],{},"\u002F\u002F Step 1: reverse entire string\n",[32,510,511],{"class":34,"line":41},[32,512,513],{},"reverse(entire_string);\n",[32,515,516],{"class":34,"line":47},[32,517,518],{},"\u002F\u002F Step 2: reverse each word individually\n",[32,520,521],{"class":34,"line":53},[32,522,523],{},"for each word in string:\n",[32,525,526],{"class":34,"line":59},[32,527,528],{},"    reverse(word);\n",[14,530,532],{"id":531},"key-takeaways","Key Takeaways",[534,535,536,540,543,546,549],"ul",{},[537,538,539],"li",{},"Iterative two-pointer swap is the most efficient general algorithm: O(n) time, O(1) extra space for mutable strings.",[537,541,542],{},"Recursive reversal is O(n²) in immutable-string languages and risks stack overflow — avoid in production.",[537,544,545],{},"Use built-in reverse functions whenever available; they are optimized and well-tested.",[537,547,548],{},"In-place reversal only saves memory for mutable string types (C, C++); in other languages, all approaches allocate O(n) anyway.",[537,550,551],{},"Word-level reversal is a distinct problem best solved by splitting and rejoining, or by the two-step reverse method.",[14,553,555],{"id":554},"try-it-yourself","Try It Yourself",[10,557,558,559,564],{},"Want to experiment with string reversal across different inputs? Paste any text into the ",[560,561,563],"a",{"href":562},"\u002Ftools\u002Ftext-reverser","Text Reverser"," and see the reversed output instantly — including how it handles punctuation, numbers, and multi-line content.",[566,567,568],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":27,"searchDepth":41,"depth":41,"links":570},[571,572,573,574,575,576,577],{"id":16,"depth":41,"text":17},{"id":151,"depth":41,"text":152},{"id":233,"depth":41,"text":234},{"id":362,"depth":41,"text":363},{"id":460,"depth":41,"text":461},{"id":531,"depth":41,"text":532},{"id":554,"depth":41,"text":555},"2026-05-28","Iterative, recursive, and functional approaches to reversing strings across languages.","md",{"immutable":582},true,"\u002Fguides\u002Fstring-reversal-algorithms",{"title":5,"description":579},"guides\u002Fstring-reversal-algorithms","u9Z6SN9tQVr1WfqESf-oNIZ348ImcC4B3CHfg_-JIY4",1780401337207]