ping pong with two Arduinos over serial
The problem is with your protocol and also with how you are using the serial.

You are using the same serial channel both for your communication protocol and for the reporting of the results.  What you see the B end sees.

So A sends ping, and when it sees the letter g it responds with pong.  You then print that pong to the serial, so end B sees pong. It gets the character g and thinks "That was a ping, I'll respond with a pong", so it does - it sends pong, which you then receive and duly print - starting the sequence over yet again.

You either need to separate out the communication and the reporting, or make your protocol look for the whole string "ping" so it never inadvertently responds to a "pong" when it shouldn't.

Here's a good resource about reading serial strings on the Arduino: http://hacking.majenko.co.uk/reading-serial-on-the-arduino