What is the purpose of resistor in charging and discharging of capacitor?
The two answers you have are both correct, but each assumes a different circuit. Since you felt it wise to hide the details, you force us to guess. First answer assumes you are using a resistor in series with a switch to discharge or charge the cap, where the resistor is needed to limit the current to a value that won’t damage the switch or the capacitor or melt the wires. Second answer assumes the resistor is placed directly across the cap permanently so that that the cap will discharge when the power is turned off and prevents a shock hazard. .