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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* reverseString(char input[])
{
int length = strlen(input);
int i;
char* reversedString
= (char*)malloc(length * sizeof(char));
for (i = length - 1; i >= 0; i--)
reversedString[length - 1 - i] = input[i];
return reversedString;
}
int main(int argc, char* argv[])
{
if (argc == 1)
printf("No command line arguments found.\n");
else {
printf("%s\n", reverseString(argv[1]));
}
return 0;
}
|
Java
class GFG {
public static String reverseString(String input)
{
String reversedString = "";
for (int i = input.length() - 1; i >= 0; i--)
reversedString += input.charAt(i);
return reversedString;
}
public static void main(String[] args)
{
if (args.length > 0) {
System.out.println(reverseString(args[0]));
}
else
System.out.println("No command line "
+ "arguments found.");
}
}
|
Python
import sys
def reverseString(input_str):
reversed_str = input_str[::-1]
return reversed_str
if __name__ == "__main__":
if len(sys.argv) == 1:
print("No command line arguments found.")
else:
print(reverseString(sys.argv[1]))
|
Output:
- In C:

- In Java:

In Python

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 string
- Swap the characters from one end with the characters from the other end
- This is the required reversed string
Program:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* reverseString(char str[])
{
int n = strlen(str);
int i;
char temp;
for (i = 0; i < n / 2; i++) {
temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
return str;
}
int main(int argc, char* argv[])
{
if (argc == 1)
printf("No command line arguments found.\n");
else {
reverseString(argv[1]);
printf("%s\n", argv[1]);
}
return 0;
}
|
Java
class GFG {
public static String reverseString(String str)
{
int n = str.length();
char temp;
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;
}
public static void main(String[] args)
{
if (args.length > 0) {
System.out.println(reverseString(args[0]));
}
else
System.out.println("No command line "
+ "arguments found.");
}
}
|
Output:
- In C:

- In Java:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
15 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...