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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:36:16+00:00 2026-06-10T23:36:16+00:00

I am trying to reverse a null terminated string in place in C++. I

  • 0

I am trying to reverse a null terminated string in place in C++. I have written the code below:

//Implement a function to reverse a null terminated string

#include<iostream>
#include<cstdlib>

using namespace std;
void reverseString(char *str)
{
    int length=0;
    char *end = str;
    while(*end != '\0')
    {
        length++;
        end++;
    }
    cout<<"length : "<<length<<endl;
    end--;

    while(str < end)
    {

        char temp = *str;
        *str++ = *end;
        *end-- = temp; 


    }

}
int main(void)
{
    char *str = "hello world";
    reverseString(str);
    cout<<"Reversed string : "<<str<<endl;
}

However, when I run this C++ program , I get a a write access violation inside the while loop at the statement : *str = *end ;

Even though this is fairly simple, I can’t seem to figure out the exact reason I am getting this error.

Could you please help me identify the error?

  • 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-06-10T23:36:18+00:00Added an answer on June 10, 2026 at 11:36 pm
    char *str = "hello world";
    

    is a pointer to a string literal, and can’t be modified. String literals reside in read-only memory and attempting to modify them results in undefined behavior. In your case, a crash.

    Since this is clearly an assignment, I won’t suggest using std::string instead, since it’s good to learn these things. Use:

    char str[] = "hello world";
    

    and it should work. In this case, str would be an automatic-storage (stack) variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to implement curl effect by using below code snippet curl down
I'm trying to understand if this code below creates 12 objects for a string
I am trying to reverse a linked list. This is the code I have
I have been trying to reverse a Mach-O executable arm file and I'm able
I am learning C and I'm trying to reverse each string in an array
I am trying to find a way to reverse a string, I've seen alternatives
I am trying to reverse a character string in C Here is what I
I am trying to reverse a C style string using the following simple program.
I'm trying to reverse items in a dictionary in C#. I have tried: Dictionary<double,
I am writing a Java function to reverse a linked list in-place. I am

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.