Given a string, the task is to check if this String is Palindrome or not using Command Line Arguments.
Examples:
Input: str = "Geeks"
Output: No
Input: str = "GFG"
Output: Yes
Approach:
- 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
- Check if the characters from one end match with the characters from the other end
- If any character do not matches, the String is not Palindrome
- If all character matches, the String is a Palindrome
Program:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isPalindrome( char * str)
{
int n = strlen (str);
int i;
for (i = 0; i < n / 2; i++)
if (str[i] != str[n - i - 1])
return 0;
return 1;
}
int main( int argc, char * argv[])
{
int res = 0;
if (argc == 1)
printf ( "No command line arguments found.\n" );
else {
res = isPalindrome(argv[1]);
if (res == 0)
printf ( "No\n" );
else
printf ( "Yes\n" );
}
return 0;
}
|
Java
class GFG {
public static int isPalindrome(String str)
{
int n = str.length();
for ( int i = 0 ; i < n / 2 ; i++)
if (str.charAt(i) != str.charAt(n - i - 1 ))
return 0 ;
return 1 ;
}
public static void main(String[] args)
{
if (args.length > 0 ) {
int res = isPalindrome(args[ 0 ]);
if (res == 0 )
System.out.println( "No\n" );
else
System.out.println( "Yes\n" );
}
else
System.out.println( "No command line "
+ "arguments found." );
}
}
|
Output:- In C:

- In Java:
