Answer to Question #249688 in C++ for vishal

Question #249688

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


1
Expert's answer
2021-10-12T00:37:18-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS