Praise be to Allah.
The Shaytan is keen to corrupt the Muslim’s worship by means of waswas (insinuating whispers), but the Muslim can ward off these tricks of the Shaytan by seeking refuge in Allah, may He be exalted, from his evil, and learning the rulings of Islam and acquiring knowledge of fiqh (jurisprudence). If he starts to doubt how many rak’ahs he has done during prayer, he should proceed on the basis of the smaller number and do the prostration of forgetfulness before saying the salam. If he is not sure whether he divorced his wife or not, the basic principle is that he did not divorce her. If he is not sure whether there is any impurity (najasah) on his body or clothes or not, the basic principle is that his body and clothes are pure, and so on.
These rulings apply to cases of doubt that do not stem from waswas. If the doubt is caused by waswas, then he should not pay attention to anything that the Shaytan casts into his mind, and there is no other solution but to seek refuge with Allah from the Shaytan and ignore this waswas, and not pay any attention to it. Similarly, after doing wudu and praying, if he feels uncertain about them, he should not pay any attention to that uncertainty.
If this waswas is not warded off by seeking refuge with Allah, and it overwhelms the Muslim until he says or does something without realizing, that is compulsive waswas or OCD (al-waswas al-qahri). This is a kind of sickness and he should seek treatment for it by means of ruqyah (recitations) from the Quran and Sunnah as prescribed in Islam, and permissible medicines. There is no reason why he should not go to a Muslim doctor whose religious commitment and knowledge of Islam can be trusted.
If this person who is affected by waswas experiences physical movements that he cannot control or he says things that he cannot stop himself saying, then there is no sin on him and he is excused according to Islam, because in that case he has been burdened with something that he cannot bear, so he is excused because Allah says (interpretation of the meaning):
“Allah burdens not a person beyond his scope” (al-Baqarah 2:286)
“So keep your duty to Allah and fear Him as much as you can”(al-Taghabun 64:16)
In the answer to question no. 10160, we have quoted the words of Shaykh Muhammad ibn Salih al-‘Uthaymeen (may Allah have mercy on him):
Similarly, if there crosses his mind any thought of impugning Allah (may He be exalted) or impugning the Quran or other kinds of kufr (disbelief), he should not pay any attention to it, and it will not harm him, and even if we assume that he says such things involuntarily, there will be no sin on him. End quote.
For more information on your situation and on how to treat it by means of ruqyah prescribed in Islam and adhkar (words of remembrance) narrated from the Prophet (peace and blessings of Allah be upon him), please see the following questions: 39684, 10160, 59931, 62839, 25778 and 12315.
These are of benefit for those who are affected by waswas and we hope that you will benefit from them. We ask Allah to heal you and make you well.
And Allah knows best.