Saturday, 10 September 2011

reverse string with c++

  • #include <iostream>
  • #include <string>
  • using namespace std;
  •  
  • void recursiveReverse( char a[], int start, int end )
  • {
  • int i, j;
  •  
  • for(i=start; i<end; i++)
  • {
  •  
  • for(j=end;j>start;j--)
  • {
  • char temp = a[i];
  • a[i]=a[j];
  • a[j]=temp;
  • }
  •  
  • }
  •  
  • }
  •  
  •  
  •  
  • void stringReverse( char str[] )
  • {
  • int length = strlen( str );
  • recursiveReverse( str, 0, length - 1 );
  • }
  •  
  • int main()
  • {
  •  
  • char a[20];
  •  
  • for (int i = 0; i < 20; i++ )
  • a[i] = 'A' + i;
  • a[19] = '\0';
  • cout << "string before reversing: " << a << endl;
  • stringReverse( a );
  • cout << "string after reversing: " << a << endl;
  • return 0;
  • No comments:

    Post a Comment