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 7428353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:44:00+00:00 2026-05-29T08:44:00+00:00

I have the following code in c# which reverses a string: char[] charArray =

  • 0

I have the following code in c# which reverses a string:

char[] charArray = s.ToCharArray();
int len = s.Length - 1;

for (int i = 0; i < len; i++, len--)
{
   charArray[i] ^= charArray[len];
   charArray[len] ^= charArray[i];
   charArray[i] ^= charArray[len];
}

return new string(charArray);

I am trying to convert it to C++ as an intellectual exercise more than anything. Here is what I have so far:

void main(void)
{
    char* str = "testing";
    char* result;
    int len;

    len = strlen(str);

    if (len <= 12)
    {
        result = new char[strlen(str)];
        for (int i = 0; i < len; i++, len--)
        {
             result[i] ^= str[len];
             result[len] ^= str[i];
             result[i] ^= str[len];
        }
    }
    else{
        std::reverse(str, &str[strlen(str)]);
    }

    cout << endl << result << endl;

    // cleanup 
    str = NULL;
    result = NULL;
} 

In .Net if the string is <= 12 (I think it’s twelve) xor is faster than array reverse. Source – Sam Saffron I am basically trying to see if it still holds up in C++.

The string comes out in a weird format (════╣¥¿ë²²² to be precise).

Any ideas?

Note: I know the else statement doesn’t work, I’ll figure that bit out after 😉

Note 2: I am probably doing this completely wrong, so feel free to point absolutely anything out

Update

Thanks to everyone that has participated. I haven’t played with c++ in a fair few years (and it shows) and thought it would be quiet easy to convert but obviously not. Think it’s best I abandon this idea. Thanks again

  • 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. Editorial Team
    Editorial Team
    2026-05-29T08:44:00+00:00Added an answer on May 29, 2026 at 8:44 am

    A few things:

    result = new char[strlen(str)];
    

    Should be

    result = new char[len + 1];
    

    len because you’ve already calculated the length of str, and + 1 to make room for the NUL terminator.

    Secondly, you need to copy the string into result before operating on it, because otherwise your array is full of garbage otherwise:

    strcpy(result, str);
    

    Thirdly,

    std::reverse(str, &str[strlen(str)]);
    

    Is wrong for two reasons: one, because you can’t modify string literals, and two, because you should be using result:

    std::reverse(result, result + len);
    

    But if you do that, you also need to copy str into result first.

    And lastly, setting a pointer to NULL does not deallocate the memory it points to. You have to

    delete[] result; // delete[] because new[]
    

    Note that for this to work even when the else is taken (and therefore result is not made to point to allocated memory), you need to do

    char* result = NULL; // delete[] is defined as a nop on NULL pointers
    

    All the above applies if you’re sure you want to use C-strings. Once you get the hang of pointers, you can graduate to std::string:

    std::string str("testing");
    
    std::reverse(std::begin(str), std::end(str)); // or if you don't want to do it in-place,
                                                  // std::string result(str.rbegin(), str.rend());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
I have the following code which works fine. However, I only want to return
I have the following code which iteratively reverses a linked list. struct list {
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which should put programs startable in Bash. if [
I have the following code which reads in the follow file, append a \r\n
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code which I stripped out of any non-essential lines to
I have the following code which ends up forever reading '/proc/cpuinfo' as it keeps

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.