This is a question in my college's olympiad. Is there any continuous function f(x) that gets irrational value when x is rational and gets rational value when x is irrational?

At first I thought that function doesn't exist, because I think irrational numbers are sort of "denser" than rational numbers, and I tried to prove it that way. But after over 2 weeks I'm still getting nowhere. Do you have any ideas?
I am not an expert, but…

Maybe you could assume f(x) is continuous, is irrational when x is rational, and rational when x is irrational, and try to do a proof by contradiction.

Then you could consider the function >! g(x) = f(x) - x !<

Next, you could show that >! g(x) is continuous !<

Also, >! g(x) is irrational for any x !<

From that, you can find that >! g(x) = f(x) - x is constant !<.

Therefore, >! f(x) - x = k => f(x) = x + k, where k is a constant irrational !<

However, >! f(k) = k + k = 2k. k is irrational and 2k is also irrational. This contradicts the original assumptions, so f(x) does not exist. !<

I know the proof is missing details, but I hope that someone with more knowledge can verify the method and correct any mistakes!
by
An argument using cardinalities.

f(Q) is at most countably infinite since Q is countably infinite, and f(I) is at most countably infinite by hypothesis, so f(R) = f(Q union I) is at most countably infinite. If f wasn't constant, then by the IVT, f(R) would contain an interval, which is uncountable, which cannot be.

Therefore f is constant.