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 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
void reverseWithinBounds(char[] , int , int );
void reverseCstring(char ptr[]);
int main()
{
const int SIZE = 256;
char array[SIZE]{};
int first, second;
cout << "Enter a string: ";
cin.getline(array, SIZE);
cout << array << endl;
cout << "1st element: ";
cin >> first;
cout << "2nd element: ";
cin >> second;
reverseWithinBounds(array, first, second);
cout << array << endl;
reverseCstring(array);
cout << array << endl; // why doesn't this display anything
system("pause");
return 0;
}
void reverseWithinBounds(char ptr[], int first, int second)
{
if (first >= second)
return;
swap(ptr[first], ptr[second]);
first++;
second--;
reverseWithinBounds(ptr, first , second);
}
void reverseCstring(char ptr[])
{
int start, end;
start = strlen(ptr) - strlen(ptr);
end = strlen(ptr);
reverseWithinBounds(ptr, start, end);
cout << ptr << endl; // why doesn't this display anything
}
|