In: Computer Science
Modify program so that input comes in as command line
arguments.
Sample run:
./a.out W8 4 ME 2 finish 2!
Output:
Consonants: WMfnsh
1) Name your program command_consonants.c.
2) If no command line arguments was provided, your program should
print a usage message “Usage: ./a.out input
#include<stdio.h>
int main()
{
printf("Input: ");
int i = 0;
char ch;
// array to store consonants
char consonant[100]={""};
//do loop to take input
do
{
ch = getchar();
//checks to see if consonants or not
if(((ch >= 'a' && ch <= 'z')||(ch >= 'A'
&& ch <= 'Z')) && !(ch == 'a' || ch == 'e' || ch
== 'i'|| ch == '0' || ch == 'u' || ch == 'A' || ch =='E'|| ch=='I'
|| ch =='O' || ch == 'U'))
{
consonant[i] = ch;
i++;
}
}
while(ch != '\n');
printf("Consonants:");
printf("%s", consonant);
return 0;
}
CODE -
#include<stdio.h>
int main(int argc, char* argv[])
{
int i = 0;
char ch;
// array to store consonants
char consonant[100]={""};
// Printing usage message if no command line arguments was provided.
if(argc==1)
printf("Usage: ./a.out input");
// Iterating over each space seperated string in the command line input
for(int j=1; j<argc; j++)
{
int k=0;
// do loop to read character by character of the strings in command line input
do
{
ch = argv[j][k];
//checks to see if consonants or not
if(((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z')) && !(ch == 'a' || ch == 'e' || ch == 'i'|| ch == '0' || ch == 'u' || ch == 'A' || ch =='E'|| ch=='I' || ch =='O' || ch == 'U'))
{
consonant[i] = ch;
i++;
}
k++;
}while(ch != '\0');
}
printf("Consonants: ");
printf("%s", consonant);
return 0;
}
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.