This is a non-answer answer, because I wanted to elaborate on a response to this requirement.
What you have described is not a best-practice way to use Salesforce and misses some key elements of the Sales Cloud data model.
There is no direct relationship between Contact and opportunity
As Phil pointed out, this is not true anymore - there is a direct relationship between Contact and Opportunity. More importantly, there's also the long-extant Opportunity Contact Role junction object between Contact and Opportunity. The purpose of that object is tracking relationships between Contacts and Opportunities.
Basically account is a junction object here.
Account is not a junction object, either literally or by analogy, in this scenario. Junction objects have two master-detail parents; Account is the parent object of both Contact and Opportunity.
When Account field on contact is updated, Account field on Opportunity should be updated with the same value on contact.
This is the crux of the matter and it really doesn't make sense to me. Account has a one-to-many relationship with both Contact and Opportunity. For Opportunities to "follow" Contacts as they are reparented between Accounts suggests to me either (a) the business has not thought this through or (b) they are using the wrong Account model.
If this is a B2C or nonprofit use case, where Opportunities are really linked to Contacts and Accounts are largely irrelevant, the organization should be using the NPSP Household Model or Person Accounts or something similar, not trying to move Opportunities around after Contacts.
How would this scenario make sense, for example, if you have Account A with Contacts B and C, and Opportunity D, and Contact B moves to Account Q? Why does Contact C then lose their association with Opportunity D?
All that said, if you must implement this trigger as given, it's not hard. You'll have to write an after update
trigger on Contact, use Trigger.oldMap
to find Contacts that have been reparented, and then write code to reparent the Opportunities related to their old Accounts.
But I strongly recommend you don't.