Hello all

I'm having difficulty with a program I am writing to reverse the characters in a string.
The string contains a sentence where i have to change a character...if it is lower case I have to change it to upper case and visa versa. And this has to be done using a function.
The problem I am having is that I am getting an error on my if statements.

error C2451: conditional expression of type 'void' is illegal

This is parts of my code

Any toughts or suggestions would be greatly appreciated

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>


void upper_case(char[],int); // Function Uppercase Protocol
void lower_case(char[],int); // Function Lowercase Protocol

int main()
{
	char  in_put[] = "this is THE TiMe to gET thE CoRreCT ANSWERS.";
	int *message;

	printf("%s", in_put); // Print out original sentance
	printf("\n\n");

                 while(in_put[a] !='\0')
	{
		if(upper_case(in_put, a)

                                if(lower_case(in_put, a )

                                printf("%s", in_put);
	a++;
	}

                return (0);

}// end of Function main.
void upper_case(char message[], int a)// Function upper_case Definition
{

		if(isalpha(message[a]))
			if(islower(message[a]))
				message[a] = toupper(message[a]);

}
void lower_case(char message[], int a)// Function lower_case Definition
{
	if(isalpha(message[a]))
		if(isupper(message[a]))
			message[a] = tolower(message[a]);
}

Recommended Answers

All 11 Replies

The error you are getting is because of your if statements calling upper_case() and lower_case(). These 2 functions return nothing, and so the if statement can't evaluate its "trueness".

Thanks for explaining why I am getting that error

But I tried both

if(upper_case(in_put, a) = = 0))

and

if(upper_case(in_put, a) != 0))

and still get the same error

This is the first time I have had to use an If statement with a function call

To rephrase Sillyboy's explanation -- the functions do not return a value. Therefore there is no value to test == nor != against.

To rephrase Sillyboy's explanation -- the functions do not return a value. Therefore there is no value to test == nor != against.

Thanks,

I understand now what you are saying about the if statement. I rewrote the code and now when I run it I either get all lower case letters or all upper case letters depending on the order of the 2 functions.

int main()
{
	char  in_put[] = "this is THE TiMe to gET thE CoRreCT ANSWERS.";
	int *message;

	int a=0;


	printf("%s", in_put);
	printf("\n\n");

		for(a=0; a<sizeof(in_put); a++)
		{
			upper_case(in_put, a);
                           lower_case(in_put, a);
						
		}



	printf("%s", in_put);
	printf("\n\n");

	


	return (0);

}// end of Function main.

This is the results after running

this is THE TiMe to gET thE CoRreCT ANSWERS.

this is the time to get the correct answers.

OR

this is THE TiMe to gET thE CoRreCT ANSWERS.

THIS IS THE TIME TO GET THE CORRECT ANSWERS.


Depending on which function is last. Is there a way that I can test the upper_case or lower_case functions so that I can get this result?

this is THE TiMe to gET thE CoRreCT ANSWERS.

This is The Time to get the Correct Answers.

I think you may have had the correct idea initially. What you have now is checking for upper, convert to lower, and then you are checking for lower (but all the characters are already lower since you called upper_case(), so then all the characters are made changed to upper case).

While you iterate through the string you need either change to upper or change to lower, not both.

See if that gets you further...

Why devious ways to go?

char* reverse_case(char* str) {
    if (str) {
        char* p;
        int c;
        for (p = str; (c=*p) != 0); ++p)
            if (isalpha(c))
                *p = (islower(c)?toupper(c):tolower(c));
    }
    return str;
}

Why devious ways to go?

char* reverse_case(char* str) {
    if (str) {
        char* p;
        int c;
        for (p = str; (c=*p) != 0); ++p)
            if (isalpha(c))
                *p = (islower(c)?toupper(c):tolower(c));
    }
    return str;
}

I can see that would be more effecient, but we have to do it using functions and the function prototype was predefined for us.

Why devious ways to go?

Because your way is
1) too advanced for most people doing these simple problems
2) your IF cannot be understood by most novices.
IMO, your answer was more devious than a straightforward IF...ELSE .

Because your way is
1) too advanced for most people doing these simple problems
2) your IF cannot be understood by most novices.
IMO, your answer was more devious than a straightforward IF...ELSE .

Thank you for explanations, but it was a rhetorical question ;)

OK, I tried switching things around now I'm getting an assertion error.
Here is what I switched.

while(in_put[c] != '\0')
	{
		char *lttr = &in_put[c];
		if(*lttr != '\0'){
			upper_case(lttr, c);}
		else
			lower_case(lttr, c);
		printf("%c", lttr[c]);

		c++;
	}

:icon_rolleyes:

while(in_put[c] != '\0')
{
	char *lttr = &in_put[c];        // why?  Just use in_put[c]
	if(*lttr != '\0'){              // if input is not \0
		upper_case(lttr, c);}   // convert it to upper case
	else                            // if input is == \0
		lower_case(lttr, c);    // convert it to lower?  
                                        // There is no lower for \0
	printf("%c", lttr[c]);

	c++;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.