MacBook USB-C port not working until I reconnect the cable a few times

My MacBook sometimes doesn’t recognize a USB-C device until I unplug the cable and plug it back in a few times. Once it connects, it usually works fine, but this happens often enough to be annoying. How can I tell whether it’s the cable, the port, or a software issue?