reverse string

#include <iostream>
#include <cstring>
using namespace std;

// function to reverse string str1 and copy it to string str2
void reverseString(char str1[], char str2[])
{
char *p1, *p2; // create two pointers
// Hint: Start and point both pointers to the two opposite
// ends of each string. Copy the character at one end of
// the original string to the opposite end of the new string,
// and so forth...
// Remember to add the termination character \0 at the end
// of the new string

p1=str1

}

// test function to test out reverseString()
void test1()
{
char str1[] = "Pointers are fun and easy to use";
char str2[strlen(str1)+1];
reverseString(str1, str2);
cout << "Original: " << str1 << endl;
cout << "Reversed: " << str2 << endl;
}





int main()
{
test1();
return 0;
}
i going to reverse string but i cant understand the hints given,anyone can help me?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>

int main()
{
    char str1[] = "0string1inReverse00";
    size_t length = strlen(str1);
    
    char *str2 = new char[length];
    str2[length] = '\0';
    
    char *ptr1 = &str1[0];
    char *ptr2 = &str2[length - 1];
    
    while ( *ptr1 != '\0' ) {
        *ptr2 = *ptr1;
        ptr1++;
        ptr2--;
    }
    
    std::cout << str2 << std::endl;
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.