-1

I need reverse my char string only with pointers. How can I do this? My code:

    // this cannot be modified !!!
char s[10] = "abcde";
char *pS;

// my code
pS = new char;

int count = 5;

for (int i = 0; i < 10; i++)
{
    if (s[i] != '\0') // not null
    {

        pS[count - 1] = s[i];
        count--;
    }
}

cout << "Reversed = " << pS;

Sometimes if works fine, I see only 5 chars, they are reversed. But sometimes I see some extra chars (looks like temp symbols). Where I miss something? Thank you!

5
  • 2
    You should not use hard coded numbers. Use strlen to find the length. Commented Apr 23, 2015 at 7:08
  • 2
    pS = new char; allocates space for a single char, not an array. Commented Apr 23, 2015 at 7:08
  • Are you supposed to create a new string or reverse the original in place? It's unclear what "this cannot be modified" means. Commented Apr 23, 2015 at 7:12
  • To molbdnilo: I need store reversed string in pS variable To CreativeMind: this is just test code, I want fully understand pointers:) To πάντα ῥεῖ: yep, I undersnad. But how my pS still store reversed string with allocation for one char? Commented Apr 23, 2015 at 7:36
  • @VladyslaveSemenchenko "But how my pS still store reversed string with allocation for one char?" It's just undefined behavior to access unallocated memory, may or may not "work". Commented Apr 23, 2015 at 7:50

3 Answers 3

2

your char array "s" contains 10 chars, but you only initialize the first 6 chars of that array with "abcde" and the \0 terminator. When you loop over the complete array, you access not initialized chars.

I also see, that you try to write to memory, which you didn't allocate. You only allocate memory for 1 char for you "pS" pointer, but you try to access it's memory like it is an array of chars in your for-loop.

Instead of using hardcoded:

int count = 5;

you also could use the string function strlen() to determine the length of the c-string.

Edited (untested code):

char s[10] = "abcde";
char pS[10];

for (int i = 0; i < strlen(s); i++)
{
    if (s[i] == '\0') // not null
    {
        // stop loop, as soon as you reach the end of the original string
        break;
    }
    pS[strlen(s) - 1 - i];
}

// now add the termination char \0 to your pS array
pS[strlen(s)] = '\0';

cout << "Reversed = " << pS;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your unswear. How can I allocate memory for say 5 chars?
@VladyslaveSemenchenko "How can I allocate memory for say 5 chars?" Like this pS = new char[5]; Don't forget to call delete [] pS;, if it's no longer in use.
To πάντα ῥεῖ: Hmm..still see extra symbols. So my Ts string looks like edcba then some temp symbols. Looks like I wrong allocate memory:(
you probably just forgot the termination char for your new "array". So the "cout <<" doesn't know, when to stop writing the string ... until it reaches a \0 in the memory.
1

Just giving you the hint how to reverse the string using pointers:

  1. Take two pointers front and rear where front is pointing to first char of string and rear is pointing to last char of string.
  2. Check if front is less than rear
  3. If yes, swap the value of first and last character. If no , just print the string.
  4. Increment front pointer and decrement rear pointer
  5. Repeat from step 2.

Comments

0

After reading another book I fully understand pointers and how to correctly allocate memory. Here is my final code which correctly reverse array of char string (I don't need universal code, just working example + without std methods for reversing):

// not edited part - based on exercise (I mean I cannot change pS to char[5] etc.
char s[10] = "abcde";
char *pS;

pS = new char[strlen(s) + 1]; // allocate correct memory size based on string size

cout << "Size is " << sizeof(pS) << endl; // just for testing
int count = strlen(s); // for iteration

pS[count] = '\0'; // last symbol must be '\o' (thanks to Mr.Yellow)

for (int i = 0; i < 10; i++) // 10 because array of char still has 10 elements
{
    if (s[i] != '\0') // looks like "not garbage memory"
    {
        count--;
        pS[count] = s[i]; // set correct value
    }
}

cout << "Reversed = " << pS << endl;

Thank you to all who helps me!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.