[Solved] Command Line Argument Count with Java, C++

Command Line Argument Count: 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

Solution

class Main
{
    public static void main(String jr[])
    {
        System.out.println("Arguments :");
        int count = 0;
        for(int i=0;i<jr.length;i++)
        {
            System.out.println(jr[i]);
            count++;
        }
        System.out.println("Number of arguments is "+count);
    }
}
#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
    cout << "Arguments :" << endl;
    for (int i = 1; i < argc; i++)
    //sauravhathi
    {
        cout << argv[i] << endl;
    }
    cout << "Number of arguments is " << argc - 1 << endl;
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *