14. Command Line Argument
/--------------------------------------------------------------------------/
(1)
#include<stdio.h>
#include<string.h>
int my_atoi(const char *nptr);
int main(int argc,char **argv)
{
printf("%d\n",my_atoi(argv[1]));
}
int my_atoi(const char *nptr)
{
int len,i,j,k,c,num1;
len=strlen(nptr);
for(i=0,num1=0;i<len;i++)
{
for(c=0,j=48,k=nptr[i];j<=57;j++,c++)
if(k==j)
num1=num1*10+c;
}
return num1;
}
/--------------------------------------------------------------------------/
(2)
#include<stdio.h>
#include<string.h>
int my_atoi(const char *nptr);
int main(int argc,char **argv)
{
int c,j,k;
for(c=0,j=48,k=argv[1][0];j<=57;j++,c++)
if(k==j)
printf("%d\n",c);
}
/--------------------------------------------------------------------------/
(3)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char **argv)
{
int num1,num2,i;
num1=atoi(argv[1]);
num2=atoi(argv[2]);
while(num1<=num2)
{
for(i=2;i<num1;i++)
{
if(num1%i==0)
break;
}
if(i==num1)
printf(" %d ",num1);
num1++;
}
printf("\n");
}
/--------------------------------------------------------------------------/
(4)a
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char **argv)
{
int num1,num2,i,sum;
num1=atoi(argv[1]);
num2=atoi(argv[2]);
while(num1<=num2)
{
for(i=1,sum=0;i<num1;i++)
{
if(num1%i==0)
sum=sum+i;
}
if(sum==num1)
printf(" %d ",num1);
num1++;
}
printf("\n");
}
/--------------------------------------------------------------------------/
(4)b
#include<stdio.h>
#include<string.h>
int main(int argc,char **argv)
{
int i,j;
char temp[10];
for(i=1,j=argc-1;i<j;i++,j--)
{
strcpy(temp,argv[j]);
strcpy(argv[j],argv[i]);
strcpy(argv[i],temp);
}
for(i=1;i<argc;i++)
printf(" %s ",argv[i]);
printf("\n");
}
/--------------------------------------------------------------------------/
(5)
#include<stdio.h>
#include<stdlib.h>
double my_atof(const char *);
void main(int argc,char **argv)
{
int flag,i;
if(argc<2)
printf("Usage: a.out String \n");
double num = my_atof(argv[1]);
printf("%f\n",num);
}
double my_atof(const char *p)
{
int i;
float c,num,num1;
if(p[0]=='-' || p[0]=='+')
i=1;
else
i=0;
for(num=0;p[i];i++)
{
if(p[i]>'0' && p[i]<'9')
{
num=num*10+(p[i]-48);
}
else if(p[i]=='.')
break;
else
goto L1;
}
i++;
for(num1=0,c=1;p[i];i++)
{
if(p[i]>'0' && p[i]<'9')
{
num1=num1*10+(p[i]-48);
c=c*10;
}
else
break;
}
num1=num1/c;
num=num + num1;
if(p[0]=='-')
num=-num;
L1:
return num;
}
/--------------------------------------------------------------------------/
(6)
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char **argv)
{
int i;
float arr[argc],sum,avg;
for(i=1;i<argc;i++)
arr[i]=atof(argv[i]);
for(i=1,sum=0;i<argc;i++)
sum=sum+arr[i];
avg=sum/(argc-1);
printf("Sum = %f \n",sum);
printf("Avg = %f \n",avg);
}
/--------------------------------------------------------------------------/
(7)
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char **argv)
{
if(argc!=4)
printf("Enter valid arguments");
int n1,n2;
n1=atoi(argv[1]);
n2=atoi(argv[3]);
switch(argv[2][0])
{
case '+' : printf(" Sum = %d\n",n1+n2);
break;
case '-' : printf(" Sub = %d\n",n1-n2);
break;
case '*' : printf(" Mul = %d\n",n1*n2);
break;
case '/' : printf(" Div = %d\n",n1/n2);
break;
}
}
/--------------------------------------------------------------------------/
(8)
#include<stdio.h>
#include<string.h>
void my_strrev(char *p);
int main(int argc,char **argv)
{
my_strrev(argv[1]);
printf("%s\n",argv[1]);
}
void my_strrev(char *p)
{
char *q;
q=p;
while(*q)
q++;
q--;
while(p<q)
{
*p=*p^*q;
*q=*p^*q;
*p=*p^*q;
p++;
q--;
}
}
/--------------------------------------------------------------------------/
(9)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s[10],in,ch;
int len,i;
void main(int argc,char **argv)
{
strcpy(s,argv[1]);
len=strlen(s);
in=atoi(argv[2]);
ch=argv[3][0];
for(len=0;s[len];len++);
for(i=len;i>=in;i--)
{
s[i+1]=s[i];
}
s[in]=ch;
printf("%s",s);
}
/--------------------------------------------------------------------------/
(10)
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char **argv)
{
int input,i,j,k,s;
input=atoi(argv[1]);
for(i=1;i<=input;i++)
{
for(k=i,j=1;k<input;k++)
j=j+2;
for(s=1;s<i;s++)
printf(" ");
while(j>=1)
{
printf("%d ",j);
j=j-2;
}
printf("\n");
}
}
/--------------------------------------------------------------------------/
(11)
#include<stdio.h>
char *p,digits=0,i;
void main(int argc,char **argv)
{
for(p=argv[1];*p;p++)
{
if(*p>='0' && *p<='9')
digits++;
}
printf("Digits are : %d\n",digits);
}
/--------------------------------------------------------------------------/
(12)
#include<stdio.h>
int my_strlen(char *);
int my_strcmp(char *,char *);
void main(int argc,char **argv)
{
int len1,len2;
len1=my_strlen(argv[1]);
len2=my_strlen(argv[2]);
printf("len1 = %d\n",len1);
printf("len2 = %d\n",len2);
my_strcmp(argv[1],argv[2]) ? printf("Both strings are equal\n"):printf("Both strings are not equal\n");
}
int my_strlen(char *p)
{
int c=0;
while(*p)
{
c++;
p++;
}
return c;
}
int my_strcmp(char *p,char *q)
{
while(*p)
{
if(*p != *q)
return 0;
p++;
q++;
}
return 1;
}
/--------------------------------------------------------------------------/
Comments
Post a Comment