Guest User

Untitled

a guest
Sep 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. /*
  2. * reverseString takes a single command line argument as a string and
  3. * reverses it.
  4. *
  5. * Example: "123" is reversed to "321"
  6. *
  7. * Time/space complexity: O(n)
  8. *
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. int main (int argc, char* argv[]) {
  15.  
  16. if (argc != 2) {
  17. printf ("\n\tOops! You might want to give me one string to reverse.\n\n");
  18. } else {
  19.  
  20. char temp = 0;
  21. int tail = strlen(argv[1]) - 1;
  22.  
  23. printf ("\nOriginal string = %s\n", argv[1]);
  24.  
  25. for (int head = 0; head < tail; head++, tail--)
  26. {
  27. temp = argv[1][head];
  28. argv[1][head] = argv[1][tail];
  29. argv[1][tail] = temp;
  30. }
  31.  
  32. printf ("Reversed string = %s\n", argv[1]);
  33. }
  34. }
Add Comment
Please, Sign In to add comment