Question

Write a C program which asks the user to enter a name of a person and...

Write a C program which asks the user to enter a name of a person and the program finds the number of that person. The number is displayed. Otherwise output "Not found". Your program have to check only given names.
The program declares and initializes a two-dimensional array of characters that holds pairs of names and numbers. The array can hold 10 strings, each capable of holding up to 79 characters. Array elements are following:
"Adam", "585-5622",
"Mark", "505-8446",
"Austin", "955-1055",
"Satish", "444-1490",
"James", "555-8873"
Hint:Use strcmp().

Sample input:
Austin
Sample output:
955-1055

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<string.h>
int main()
{
    //initialize the two diensional character array by 10 names and numbers
    char name[10][79]={"Adam","585-5622","Mark","505-8446", "Austin","955-1055","Satish","444-1490","James","555-8873"};
    int i,opt;
    char nm[79];
    //ask for a name to search
    printf("\n Enter the name");
    gets(nm);
    //loop to scan the array elements
    //the step value is i+=2 because at even position inly the names are occupied
    for(i=0;i<10;i+=2)
    {
       //compare the users entry name awith the existing data in name[]
       if(strcmp(nm,name[i])==0)
       {//if found then set opt=1 and terminate the loop by break;
           opt=1;
           break;
       }
   }
   
    //if opt=1 then display the number of that person
    if(opt==1)
       printf("\n The number is : %s",name[i+1]);
    else
    //print data not fund if opt!=1
      printf("\n Not Found");  
   
    }

output

Add a comment
Know the answer?
Add Answer to:
Write a C program which asks the user to enter a name of a person and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT