Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Write a program that receives some info from the console about people and their phone numbers. Each entry
- //should have just one name and one number (both of them strings).
- //On each line, you will receive some of the following commands:
- // A {name} {phone} – adds entry to the phonebook. In case of trying to add a name that is already in the
- //phonebook you should change the existing phone number with the new one provided.
- // S {name} – searches for a contact by given name and prints it in format "{name} -> {number}". In case
- //the contact isn't found, print "Contact {name} does not exist.".
- // END – stop receiving more commands.
- $phonebook = array();
- while(($entry = readline())!="END")
- {
- $contacts = explode(" ", $entry);
- if ($contacts[0] == "A") {
- // case "A" :
- //echo "adding";
- $name = $contacts[1];
- $number = $contacts[2];
- $phonebook[$name] = $number;
- } elseif ($contacts[0] == "S") {
- //echo "search";
- $name = $contacts[1];
- //echo $name."$^
- if (array_key_exists($name, $phonebook)) {
- //echo "/test1";
- echo $name . " -> " . $phonebook[$name]."\n";
- } else {
- //var_dump($phonebook);
- echo "Contact $name does not exist.\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement