-
Uppercase & Lowercase
How can I modify the codes to be able to see the outputs 1-lowercase and 2-Uppercase?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char **argv)
{
int (*convcase[2])(int) = {toupper, tolower};
int func;
int result = EXIT_SUCCESS;
int ch;
if(argc > 0)
{
if(toupper((unsigned char)argv[0][0]) == 'U')
{
func = 0;
}
else
{
func = 1;
}
while((ch = getchar()) != EOF)
{
ch = (*convcase[func])((unsigned char)ch);
putchar(ch);
}
}
else
{
fprintf(stderr, "Unknown name. Can't decide what to do.\n");
result = EXIT_FAILURE;
}
return result;
}
[code][/code]tagged by Salem
-
Too many brackets. "else" cannot follow "while". And you won't get an EOF from a carriage return ;) Use '\n' instead. Fix that and it will work fine.
-
Code:
if(toupper((unsigned char)argv[0][0]) == 'U')
argv[0] is the name of your program
the first argument is argv[1]