Write a program to accept strings as command line argument and print the number of arguments entered.
Sample Input (Command Line Argument) 1:
Command Arguments
Sample Output 1:
Arguments :
Command
Arguments
Number of arguments is 2
Sample Input (Command Line Argument) 1:
Commands
Sample Output 2:
Arguments :
Commands
Number of arguments is 1
Answers
Answer:
#include <stdio.h>
void main(int argc, char *argv[] ) {
printf("Program name is: %s\n", argv[0]);
if(argc < 2){
printf("No argument passed through command line.\n");
}
else{
printf("First argument is: %s\n", argv[1]);
}
}
ANSWER:
java program:-
class simple
{
public static void main(String args[])
int n1,n2;
n1=args[0];
n2=args[2];
System.out.println("Arguments:"+n1+" "+n2);
System.out.println("Number of Arguments:"+count(args));
}
output:-
>>javac simple.java
>>java simple Command Arguments
Explanation:
for accepting input as string use : args[0]
for integer use : Integer.parseInt(args[0])
for boolean use : Boolean.parseBoolean(args[0])
for compiling java program : type & save java progarm using .java extension then type in command prompt >>javac simple.java
for executing java program >>java simple "str"