I was writing this code for a C program that transforms letters to lower case when called as:
./arg lower
and transforms them to upper case when invoked as:
./arg upper
Here is the code which I wrote, kindly help me out:
#include<stdio.h>
#include<ctype.h>
main(int argc,char *argv[])
{
int i;
char c;
for(i=1;i<argc;++i)
{
if(*(argv+i)=="lower")
{
while((c=getchar())!=EOF)
{
c=tolower(c);
putchar(c);
}
}
if(*(argv+i)=="upper")
{
while((c=getchar())!=EOF)
{
c=toupper(c);
putchar(c);
}
}
}
return 0;
}
You can’t do string comparisons like that in C, you need to use
strcmp(3).Better yet, use
strncmp(3)and specify the size of your buffer.Finally, for array access, you could use
argv[i]instead of*(argv+i). The meaning is the same, but the first version is much more readable.So, you’d want something like: