Umm, I can't rewrite this, but I can go through it line by line...
Code:
// I suggest not using this macro, as there is a library
// function puts.
#define puts printf
int main(int argc, char **argv)
{
int i,c_word, c_palind, c_numb; /*declaring integer type for counter*/
for(i=1;i<argc;i++)
{
// isdigit and ispunct operate on ints, not strings.
// so first off, argv[i] is a char *, not an int. You should try
// isdigit(*argv[i]), so you can pass it a char, (which
// will work as an int)
// Second, since these are for comparing characters,
// you'll never find a char that's both a digit and a punct.
// isdigit (*argv[i]) && ispunct (*argv[i])
if(isdigit(argv[i])&&ispunct(argv[i]))
// So this function will never be called.
c_numb+=check_float(argv[i]);
// isdigit (*argv[i])
else if(isdigit(argv[i]))
c_numb+=check_int(argv[i]);
}
// I don't see the reason in using 2 for loops that do the
// same conditions like this.
for(i=1;i<argc;i++)
{
// isalpha(*argv[i])
if(isalpha(argv[i]))
c_word+=check_alpha(argv[i]);
}
puts(" There were %d argument(s) \n ", argc-1 ); /* total number of arguments */
puts(" %d word(s) \n ", c_word ); /* total number of words */
//puts(" %d palindrom(s)\n ", c_palin); /* total number of palindromes */
puts(" %d number(s) \n ", c_numb ); /* total number of numbers */
return 0;
}
int check_float(char argv[])
{
float a = 0.0;
a = atof(argv);
printf("%f",a);
// The floats may be stored with round off errors so
// that this is not accurate...
if((a%2.0)==0.0)
puts("The number %f is a real number that is even\n",a);
else
puts("The number %f is a real number that is odd\n",a);
return 1;
}
int check_int(char argv[])
{
int b = 0;
b = atoi(argv);
if((b%2)==0)
// You're passing a, but you need to pass b. That's why this
// function won't work.
puts("The number %d is a integer number that is even\n",a);
else
puts("The number %d is a integer number that is odd\n",a);
return 1;
}
int check_alpha(char argv[])
{
puts("%s\n", argv);
return 1;
}
The only thing I'm not ure about is the isdigit(), ispunct() stuff, since you're using C++, those may operate differently, but as it is in C, those will only check the first character, and you have to pass them an int or a char, not a char *.