Command Line Argument - Count
Write a program to accept strings as command-line arguments and print the number of arguments entered.
Sample Input (Command Line Argument) 1:
Command Arguments
Sample Output 1:
Arguments :
Command
Arguments
The number of arguments is 2
Sample Input (Command Line Argument) 2:
Commands
Sample Output 2:
Arguments :
Commands
The number of arguments is 1
#include <bits/stdc++.h>
using namespace std;
void argument(string str)
{
string W = "";
for (auto x : str)
{
if (x == ' ')
{
cout << W << endl;
W = "";
}
else {
W = W + x;
}
}
cout << W << endl;
}
int main()
{
char s[20];
int count = 0, i;
cout<<"Enter a string: ";
gets(s);
cout<<"Arguments:"<<endl;
argument(s);
for (i = 0; s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
cout << "\nNumber of words in the string are: " << count + 1;
return 0;
}
Comments
Leave a comment