Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:23:37+00:00 2026-05-10T17:23:37+00:00

How do you reverse a string in C or C++ without requiring a separate

  • 0

How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:23:37+00:00Added an answer on May 10, 2026 at 5:23 pm

    The standard algorithm is to use pointers to the start / end, and walk them inward until they meet or cross in the middle. Swap as you go.


    Reverse ASCII string, i.e. a 0-terminated array where every character fits in 1 char. (Or other non-multibyte character sets).

    void strrev(char *head) {   if (!head) return;   char *tail = head;   while(*tail) ++tail;    // find the 0 terminator, like head+strlen   --tail;               // tail points to the last real char                         // head still points to the first   for( ; head < tail; ++head, --tail) {       // walk pointers inwards until they meet or cross in the middle       char h = *head, t = *tail;       *head = t;           // swapping as we go       *tail = h;   } } 

    // test program that reverses its args #include <stdio.h>  int main(int argc, char **argv) {   do {     printf('%s ',  argv[argc-1]);     strrev(argv[argc-1]);     printf('%s\n', argv[argc-1]);   } while(--argc);    return 0; } 

    The same algorithm works for integer arrays with known length, just use tail = start + length - 1 instead of the end-finding loop.

    (Editor’s note: this answer originally used XOR-swap for this simple version, too. Fixed for the benefit of future readers of this popular question. XOR-swap is highly not recommended; hard to read and making your code compile less efficiently. You can see on the Godbolt compiler explorer how much more complicated the asm loop body is when xor-swap is compiled for x86-64 with gcc -O3.)


    Ok, fine, let’s fix the UTF-8 chars…

    (This is XOR-swap thing. Take care to note that you must avoid swapping with self, because if *p and *q are the same location you’ll zero it with a^a==0. XOR-swap depends on having two distinct locations, using them each as temporary storage.)

    Editor’s note: you can replace SWP with a safe inline function using a tmp variable.

    #include <bits/types.h> #include <stdio.h>  #define SWP(x,y) (x^=y, y^=x, x^=y)  void strrev(char *p) {   char *q = p;   while(q && *q) ++q; /* find eos */   for(--q; p < q; ++p, --q) SWP(*p, *q); }  void strrev_utf8(char *p) {   char *q = p;   strrev(p); /* call base case */    /* Ok, now fix bass-ackwards UTF chars. */   while(q && *q) ++q; /* find eos */   while(p < --q)     switch( (*q & 0xF0) >> 4 ) {     case 0xF: /* U+010000-U+10FFFF: four bytes. */       SWP(*(q-0), *(q-3));       SWP(*(q-1), *(q-2));       q -= 3;       break;     case 0xE: /* U+000800-U+00FFFF: three bytes. */       SWP(*(q-0), *(q-2));       q -= 2;       break;     case 0xC: /* fall-through */     case 0xD: /* U+000080-U+0007FF: two bytes. */       SWP(*(q-0), *(q-1));       q--;       break;     } }  int main(int argc, char **argv) {   do {     printf('%s ',  argv[argc-1]);     strrev_utf8(argv[argc-1]);     printf('%s\n', argv[argc-1]);   } while(--argc);    return 0; } 
    • Why, yes, if the input is borked, this will cheerfully swap outside the place.
    • Useful link when vandalising in the UNICODE: http://www.macchiato.com/unicode/chart/
    • Also, UTF-8 over 0x10000 is untested (as I don’t seem to have any font for it, nor the patience to use a hexeditor)

    Examples:

    $ ./strrev Räksmörgås ░▒▓○◔◑◕●  ░▒▓○◔◑◕● ●◕◑◔○▓▒░  Räksmörgås sågrömskäR  ./strrev verrts/. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's an error. There's an unconfirmed error report about it,… May 11, 2026 at 8:24 am
  • added an answer Short of detecting ' [' i.e. escaped regular expression characters,… May 11, 2026 at 8:23 am
  • added an answer If you're passing C++ objects across external library boundaries, you… May 11, 2026 at 8:23 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.
      question