Egypt and Turkey have the same basic political problem. Democracy can work despite huge ideological differences, but only if everybody is willing to be very tolerant of other people's ideas and values.

Three weeks ago the streets of Turkish cities were full of protesters demanding the resignation of Prime Minister Recep Tayyip Erdogan, who won his third straight election in 2011. Why? Because, they say, he is shoving conservative Islamic values down their throats.

The Turkish protests have now died down, but this week the streets of Egyptian cities have been full of protesters demanding exactly the same thing, for exactly the same reason. The Egyptian army has now intervened to remove the Islamist president, Mohammed Morsi, and the survival of the new Egyptian democracy is in doubt.

Neither Erdogan nor Morsi could have come to power in a country that wasn't fully democratic. Turkey has been a partly democratic country for 60 years, but if a politician with a religious agenda won, the army would remove him. It even hanged one prime minister in 1960.

In Egypt, three generals had ruled the country in unbroken succession since the mid-1950s. Latterly they allowed "elections", but their party always won, and the main religious party, the Muslim Brotherhood, was always banned.

The Turkish and Egyptian generals were mostly devout Muslims themselves, but they were willing to kill to keep religion out of politics. Islamic parties were a vehicle for traditional and anti-modern values, and the generals' goal was to modernise their countries so they would be strong enough to stand up to the West.

There was some cynicism in their policy, too. The secular political parties in Turkey (and in Egypt too, until they finally withered away under 60 years of military dictatorship) were too fragmented and disunited to pose any real threat to the army's power, whereas a single Islamic party with broad popular support might do just that. So religion must be firmly excluded from politics.

In both countries, the generals' modernising agenda had considerable success. Turkey is now a powerful middle-income nation, and at least half of its 75 million people are secular and "modern" in their political values. So they wanted the military out of politics, and finally the army withdrew - only to see the new Justice and Development (AK) Party, a "moderate" Islamist party led by Erdogan, win the 2003 election.

The Turkish generals let the AK rule because it didn't try to impose its own religious values on the whole population. It refrained because even in its best result, in 2010, the AK only won 50.3 per cent of the vote - and some of that support came from secular voters who saw it as the best hope for permanently excluding the army from politics.

Egypt is a much poorer, less educated country than Turkey, but at least a third of the 85 million Egyptians would also qualify as "modern" people with secular values. They were the ones who made the revolution happen in 2010 - but in the new democracy's first free presidential election last year the Muslim Brotherhood's candidate, Mohammed Morsi, won 51.7 per cent of the vote.

The Muslim Brotherhood promptly started writing its conservative religious values into the new constitution. More recently, Erdogan's AK Party in Turkey passed some laws that imposed its religious values too.

It wasn't a wholesale assault on the secular society - in Turkey they just placed some restrictions on the sale of alcohol - but in both countries it greatly alarmed the secular part of the population. So it took only the slightest pretext - a demonstration over the destruction of a park in Istanbul, the first anniversary of Morsi's election in Egypt - to bring huge crowds of protesters out on the street in every city.

At that point, both Islamist leaders stopped pretending they governed in the name of the entire nation. "Let them go into mosques in their shoes, let them drink alcohol in our mosques, let them raise their hand to our headscarved girls," said Erdogan of the Turkish protesters. "One prayer from our people is enough to frustrate their plans." He blamed the protests on an international conspiracy by something called the "interest-rate lobby".

In Egypt, Morsi vowed to "give my life" to defend the new constitution written by his Islamist colleagues last year, and blamed the unrest on a plot by remnants of the ousted Mubarak regime. The Egyptian army has now suspended the constitution.

The Islamists are to blame for this crisis in both countries, because their political programme does ultimately involve shoving their values down everybody else's throats. But the secular parties are also to blame, because it is their inability to unite behind a single candidate and programme that has let the Islamists win power in both Turkey and Egypt.

It is hard for democracy to survive in a country where large parts of the population hold radically different ideas about the purposes of the state and the rights of its citizens. Urbanisation will ultimately resolve this conflict, for in one more generation most of the recent immigrants to the cities will have adopted secular values. But in the meantime, Egypt will have a very rough ride. Maybe Turkey too.

* Gwynne Dyer is an independent journalist whose articles are published in 45 countries.