TCS Coding Practice Question | Reverse a String

04 Apr, 2019
Given a string, the task is to reverse this String using Command Line Arguments.
Examples:
Input: Geeks
Output: skeeG

Input: GeeksForGeeks
Output: skeeGroFskeeG

Approach 1: Using another String to store the reverse
Since the string is entered as Command line Argument, there is no need for a dedicated input line
Extract the input string from the command line argument
Create a String to store the resultant reversed string, say reversedString
Traverse through this String character by character using loop, in reverse order
Now append each character into the resultant reversedString
This is the required reversed string
Program:
C
Java
// C program to reverse a string// using command line arguments  #include <stdio.h>#include <stdlib.h>#include <string.h>  // Function to reverse the Stringchar* reverseString(char input[]){      // Get the length of the string    int length = strlen(input);    int i;      // String to store the reverse    char* reversedString        = (char*)malloc(length * sizeof(char));      // Loop through the string    // character by character in reverse order    // and store it into the resultant string    for (i = length - 1; i >= 0; i--)        reversedString[length - 1 - i] = input[i];      // Return the reversed String    return reversedString;}  // Driver codeint main(int argc, char* argv[]){    // Check if the length of args array is 1    if (argc == 1)        printf("No command line arguments found.\n");    else {          // Get the command line argument        // and reverse it        printf("%s\n", reverseString(argv[1]));    }    return 0;}
// Java program to reverse a string// using command line arguments  class GFG {      // Function to reverse the String    public static String reverseString(String input)    {          // String to store the reverse        String reversedString = "";          // Loop through the string        // character by character in reverse order        // and store it into the resultant string        for (int i = input.length() - 1; i >= 0; i--)            reversedString += input.charAt(i);          // Return the reversed String        return reversedString;    }      // Driver code    public static void main(String[] args)    {          // Check if length of args array is        // greater than 0        if (args.length > 0) {              // Get the command line argument            // and reverse it            System.out.println(reverseString(args[0]));        }        else            System.out.println("No command line "                               + "arguments found.");    }}
In C:
In Java:
Approach 2: Without using another String to store the reverse
Since the string is entered as Command line Argument, there is no need for a dedicated input line
Extract the input string from the command line argument
Traverse through this String character by character using loop till half the length of the sting
Swap the characters from one end with the characters from the other end
This is the required reversed string
Program:
C
Java
// C program to reverse a string// using command line arguments  #include <stdio.h>#include <stdlib.h>#include <string.h>  // Function to reverse a stringchar* reverseString(char str[]){      int n = strlen(str);    int i;    char temp;      // Swap character starting from two    // corners    for (i = 0; i < n / 2; i++) {        temp = str[i];        str[i] = str[n - i - 1];        str[n - i - 1] = temp;    }      return str;}  // Driver codeint main(int argc, char* argv[]){    // Check if the length of args array is 1    if (argc == 1)        printf("No command line arguments found.\n");    else {          // Get the command line argument        // and reverse it        reverseString(argv[1]);          // Print the reversed string        printf("%s\n", argv[1]);    }    return 0;}
// Java program to reverse a string// using command line arguments  class GFG {      // Function to reverse the String    public static String reverseString(String str)    {          int n = str.length();        char temp;          // Swap character starting from two        // corners        for (int i = 0; i < n / 2; i++) {            str = str.substring(0, i)                  + str.charAt(n - i - 1)                  + str.substring(i + 1, n - i - 1)                  + str.charAt(i)                  + str.substring(n - i);        }          return str;    }      // Driver code    public static void main(String[] args)    {          // Check if length of args array is        // greater than 0        if (args.length > 0) {              // Get the command line argument            // and reverse it            System.out.println(reverseString(args[0]));        }        else            System.out.println("No command line "                               + "arguments found.");    }}
Output:
In C:
In Java:
TCS
TCS-coding-questions
C Programs
Java Programs
Placements
TCS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Header files in C/C++ and its uses
C Program to read contents of Whole File
How to return multiple values from a function in C or C++?
C++ Program to check Prime Number
Producer Consumer Problem in C
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class