Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * reverseString takes a single command line argument as a string and
- * reverses it.
- *
- * Example: "123" is reversed to "321"
- *
- * Time/space complexity: O(n)
- *
- */
- #include <stdio.h>
- #include <string.h>
- int main (int argc, char* argv[]) {
- if (argc != 2) {
- printf ("\n\tOops! You might want to give me one string to reverse.\n\n");
- } else {
- char temp = 0;
- int tail = strlen(argv[1]) - 1;
- printf ("\nOriginal string = %s\n", argv[1]);
- for (int head = 0; head < tail; head++, tail--)
- {
- temp = argv[1][head];
- argv[1][head] = argv[1][tail];
- argv[1][tail] = temp;
- }
- printf ("Reversed string = %s\n", argv[1]);
- }
- }
Add Comment
Please, Sign In to add comment