-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!

  • 2
    You should not use hard coded numbers. Use strlen to find the length. – CreativeMind Apr 23 '15 at 07:08
  • 2
    `pS = new char;` allocates space for a _single_ `char`, not an array. – πάντα ῥεῖ Apr 23 '15 at 07:08
  • Are you supposed to create a new string or reverse the original in place? It's unclear what "this cannot be modified" means. – molbdnilo Apr 23 '15 at 07: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? – Vladyslav Semenchenko Apr 23 '15 at 07: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". – πάντα ῥεῖ Apr 23 '15 at 07:50

3 Answers3

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;
Mr.Yellow
  • 197
  • 1
  • 8
  • Thank you for your unswear. How can I allocate memory for say 5 chars? – Vladyslav Semenchenko Apr 23 '15 at 07:40
  • @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. – πάντα ῥεῖ Apr 23 '15 at 07:48
  • To πάντα ῥεῖ: Hmm..still see extra symbols. So my Ts string looks like edcba then some temp symbols. Looks like I wrong allocate memory:( – Vladyslav Semenchenko Apr 23 '15 at 08:06
  • 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. – Mr.Yellow Apr 23 '15 at 08:23
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.
CreativeMind
  • 897
  • 6
  • 19
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!