fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int iterations; // Number of iteration that the code will run
  5. string sentence; //The sentece that you want to reverse
  6.  
  7. void reverse ( int start, int end)
  8. {
  9. for ( int i = start, k = 0; k <= ((end-start)/2); i++, k++ ) {
  10. //swap( sentence[i], sentence[end-i] );
  11. /* This is a swap */
  12. char keep = sentence[i];
  13. sentence[i] = sentence[(end-k)];
  14. sentence[(end-k)] = keep;
  15. iterations++;
  16. }
  17.  
  18. }
  19. //4 - 7 time 7 - 4 = 3/2 = 1
  20. int main() {
  21.  
  22. sentence = "Run Time Analysis";
  23. string origin = sentence;
  24. int len = sentence.length(), start = 0, end = 0;
  25.  
  26. iterations = 0; //Starts from 0
  27.  
  28. for ( int i = 0; i < len; i++ ) {
  29. if ( sentence[i] == ' ' || i == (len-1)) {
  30. i = (i==len-1) ? (i+1) : i;
  31. end = i-1;
  32. reverse( start, end );
  33. start = i+1;
  34. }
  35. iterations++;
  36. }
  37. cout << "Orginal sentence: " << origin << "\nResult: " << sentence << "\nLength of the sentence: " << len << "\nNumber of iterations: " << iterations << "\n";
  38. return 0;
  39. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
Orginal sentence: Run Time Analysis
Result: nuR emiT sisylanA
Length of the sentence: 17
Number of iterations: 25