Praise be to Allah.
Yes, the repentance of a man who does not hope to live for long does count. This applies whether he has an incurable illness such as cancer; or he has been sentenced to death, such as a man who has been brought out for execution, even if the sword is being raised above his head; or a married man who has committed adultery and been sentenced to stoning, even if the stones have already been gathered. His repentance is valid, because Allah accepts the repentance of a man so long as the death rattle has not yet reached his throat. Allah says (interpretation of the meaning):
“Allah accepts only the repentance of those who do evil in ignorance and foolishness and repent soon afterwards; it is they to whom Allah will forgive, and Allah is Ever All-Knower, All-Wise.”
The meaning of the phrase “and repent soon afterwards” means that they repent before they die.
In the following ayah (verse), Allah says (interpretation of the meaning):
“And of no effect is the repentance of those who continue to do evil deeds until death faces one of them and he says: ‘Now I repent.’”
There are five essential conditions for repentance: sincerity, regret for what one has done, giving it up immediately, determination not to repeat it in the future, and repenting during the time when repentance will be accepted, i.e., before one dies or before the sun rises in the west.