People swear all the time. Maybe you don’t, but you probably know somebody that curses like a sailor. You don’t want to offend your readers, but you want to keep your speech realistic. What do you do?
First of all, people do swear all the time, and that includes your readers. The vast majority will not be offended. I’ve heard many complaints about Homestuck, but I’ve never heard “Dave and Karkat cuss too much.”
On the other hand, there are occasions where you absolutely cannot swear. If your story is going to be posted on or sold to a place that restricts cursing, for example, or if it’s made for children.
First of all, there are several things you should not do. Unless it’s in character, do not substitute words like “darn,” “gosh,” and “frick.” These are far from perfect substitutes. They have different connotations than the words they were designed to replace and don’t really convey the same attitude as their profane brethren.
The key is to change entire sentences instead of switching around a few words, so that the need to swear never arises at all. Have the character react to something differently than you would have otherwise, but not in a way that’s out of character. If your characters are well-developed enough, they should have several possible in-character reactions to every scenario. You want to make it look like they never considered cussing in the first place instead of having it look like they awkwardly avoided it.
This post, for example, does not contain any expletives, yet it doesn’t look like it’s purposefully avoiding them. Food for thought if you ever need to write without swearing.
EDIT: The-Firebrand brought up some good points that I forgot about:
Another route I have taken has been to create in-story curses that, to an average reader, aren’t that vulgar, but can be taken as extremely offensive to other characters. Another good option is to simply write “X Character swore”.