reversing a string crash

Hi guys I am just doing a little practice and what I thought would be pretty trivial is actually giving me some problems my program seems to crash when I try to reverse my string I can't see what I'm doing wrong everything looks right to me all memory addresses should be valid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  void reverseThree(char* str,int size){
 
   char* start = &str[0];
   char* end = &str[size-1];
   char* temp = NULL;
 
   while(start != end){
 
       *temp = *end;
       *end = *start;
       *start = *temp;
        start++;
        end--;
   }
   cout << str << endl;
}
 
int main()
{
 
    char word2[] = "hello";
    int size = sizeof(word2) / sizeof(char) -1;
    cout << endl;
    reverseThree(word2,size);
}
if you want practice, use std::string?

still, ...
size = strlen(str); //no need to pass this in.

logic...
start = 0,1,2,3,4,5
end = 5,4,3,2,1,0
now at which point above is start == end?
so then you get
6
-1 //crash!

instead, consider stopping if start >= end.


hey Jonnin

I tried changing the loop condition but now it seems like the loop doesn't even enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

void reverseThree(char* str,int size){

   char* start = &str[0];
   char* end = &str[size-2];
   char* temp;

   while(start >= end){

       cout << "in" << endl;
       *temp = *end;
       *end = *start;
       *start = *temp;

        start++;
        end--;
   }

   cout << str << endl;


}

hey Jonnin I tidied it up a little bit I even drew it out on paper and to my calculations it should work

but it still crashes here is the updated code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

void reverseThree(char* str,int size){


   char* temp;
   int i = 0;
   int j = size-2;


   while(true){

        cout << "in";

        *temp = str[i];
        str[i] = str[j];
        str[j] = *temp;

        i++;
        j--;

        if(i >= j){

            break;
        }
   }

   cout << str << endl;

}


hey Jonnin

got a solution thanks for the help :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

char* reverseThree(char* str,int size){


   char temp;
   int i = 0;
   int j = size-2;


   while(true){

        temp = str[i];
        str[i] = str[j];
        str[j] = temp;

        i++;
        j--;

        if(i >= j){

            break;
        }
   }
   return str;
}

int main()
{

    char word2[] = "hello";
    int size = sizeof(word2) / sizeof(char);
    char* hey = reverseThree(word2,size);

    hey += '\0';

    cout << "new reversed string below" << endl;

    for(int i = 0; i < size; i++){

        cout << hey[i];
    }
}


Nice. If you wanted to continue in similar style as first approach, it'd be something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using std::cout;
using std::endl;

void reverse_it(char* str, size_t sz)
{
  char* front = str;
  char* back = str+sz-1;
  char tmp;
  for(int i=0; i<sz/2; ++i)
  {
    tmp = str[i];
    *front++ = str[sz-1-i];
    *back-- = tmp;
  }
}

int main()
{
    char word[] = "semordnilaP";
    size_t sz = sizeof(word);
    reverse_it(word, sz);
    
    for(int i=0; i<sz; ++i)
      cout << word[i];
    
    cout << endl;
    
    return 0;
}

In action: https://repl.it/repls/ThriftyPopularSoftwareengineer
oh, there's apparently also a "cheating" way if you didn't also want to practice manual swaps ;D
1
2
3
4
5
void reverse_it(char* str, size_t sz)
{
  for(int i=0; i<sz/2; ++i)
    std::swap(str[i], str[sz-1-i]);
}
Or you could just use std::reverse.

1
2
3
4
void reverseThree(char* str)
{
    std::reverse(str, str + strlen(str));
}

I never knew that :O you learn something new every day thanks guys :)
hey jlb just a follow up


std::reverse only excepts Bidirectional iterators as its arguments,str is a char array?
std::reverse only excepts Bidirectional iterators as its arguments,str is a char array?

No str is a pointer to an array of char, not an array, you can use this pointer as shown to reverse the array.


thanks jlb

it does work great,but I'm surprised it works :o it says that it only takes bidirectional iterators as arguments yet it works with pointers aswell?
http://www.cplusplus.com/reference/algorithm/reverse/

also the second argument in the reverse function str + strlen(str)

I'm guessing the first argument is where the str starts or the address where it starts and the str + strlen(str) is where the str ends right?


thanks :)
Last edited on
it says that it only takes bidirectional iterators as arguments yet it works with pointers aswell?

Yes in this circumstance any differences between iterators and pointers are not important. Remember an iterator acts a lot like a pointer in many circumstances.

See this link for more information:
https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/iterators.html

I'm guessing the first argument is where the str starts or the address where it starts

Yes.

the str + strlen(str)

Yes this points to the end of the C-string.





Last edited on
thanks jlb,I never knew that I'm actually very surprised
Topic archived. No new replies allowed.