Question

(c programming): write a program that contains a function named getName, that does not get any...

(c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name.

each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white" characters such as spaces, tabs, newline(\n) etc.

note: the function returns a name(character array) and not an index.

to choose a random name from the list, use the function rand from stdlib.h

additionally, in MAIN() you should read 30 names, and initialize them accordingly. then call getName 10 times and print the randomly chosen name.Note: the names read in main should be different from each other, and the program should not differentiate between lower and uppercase, so for instance if given: "ABC" "aBC" "abC" - they should be considered the same and the program should terminate while outputting which name was given twice.

again, if a name was given twice, even with lower/uppercase, the program should terminate, informing the user he has given the same name twice and outputting which name.

please also make sure that the name does not include \n in the end of the input string.

the input comes from stdin and can come from a file(using redirection) or keyboard.

the program should output a friendly request for an input, and output the names given, before calling getName

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

Program:

------------------------


#include <stdio.h>
#include <string.h>
#define RAND_MAX 30
/*Global array to hold name*/
char names[RAND_MAX][20];

//Function getName that returns random name from global array
char *getName()
{
    int num = rand()%RAND_MAX;
    return names[num];
}

/*Main Function*/
int main()
{
    int i=0;
    int j=0;
    //request to input names
   printf("Please enter 30 distinct name of your choice, each name seperated by Enter\n");
   //loop to read all the 30 names
   for(i=0;i<30;i++)
   {
       printf("Input For name%d: ",i+1);
       gets(names[i]);
       //loop to check if the name already present
       for (j=0;j<i;j++)
       {
           if(strcasecmp(names[i],names[j]) ==0 )
           {
            printf("The name %s is inputted twice",names[i]);
            exit(0);
           }
       }
   }
   //Print inpuuted names
   printf("\n*****************\nThe names inputted are \n****************");
   for(i=0;i<30;i++)
   {
       printf("\n%d)%s",(i+1),names[i]);
   }


   printf("\n***************\nThe names retrieved by calling getName function are\n**************");
   for(i=0;i<10;i++)
   {
       //Print the name that is retured using getName function
       printf("\n%d)%s",(i+1),getName());
   }
    printf("\n****Program completed successfully....****");
    return 0;
}

Output:

-----------------------

Please enter 30 distinct name of your choice, each name seperated by Enter
Input For name1: Iswarya D
Input For name2: Rams a Sri
Input For name3: Vara K Lakshmi
Input For name4: Jagan Mohan
Input For name5: Sri Devi
Input For name6: Sit risha Siri
Input For name7: Shekhar Pyne
Input For name8: Shekhar Reddy
Input For name9: Wrishin Sarkar
Input For name10: Charbak Roy
Input For name11: Pintu Debnath
Input For name12: Srikar Reddy
Input For name13: Sunith Singh
Input For name14: Manas Nayak
Input For name15: Mani Preeth
Input For name16: Shanuga A saradhu i
Input For name17: Alina Geeta
Input For name18: Ma egha megha
Input For name19: Sravanthi Sravs
Input For name20: Navya Dandu
Input For name21: Leela Narra
Input For name22: Pradhyuman Miyar
Input For name23: Parabs esh Das
Input For name24: Praveen Ki umar
Input For name25: P Bhanu Praksh ah
Input For name26: Agnes Martinen
Input For name27: Roger Clarck
Input For name28: Carolina Rocha
Input For name29: Kasturi Biswar s
Input For name30: Soham Ray

*****************
The names inputted are
****************
1)Iswarya D
2)Rama Sri
3)Vara Lakshmi
4)Jagan Mohan
5)Sri Devi
6)Sirisha Siri
7)Shekhar Pyne
8)Shekhar Reddy
9)Wrishin Sarkar
10)Charbak Roy
11)Pintu Debnath
12)Srikar Reddy
13)Sunith Singh
14)Manas Nayak
15)Mani Preeth
16)Shanuga saradhi
17)Alina Geeta
18)Megha megha
19)Sravanthi Sravs
20)Navya Dandu
21)Leela Narra
22)Pradhyuman Miyar
23)Parabesh Das
24)Praveen Kumar
25)Bhanu Praksah
26)Agnes Martinen
27)Roger Clarck
28)Carolina Rocha
29)Kasturi Biswas
30)Soham Ray
***************
The names retrieved by calling getName function are
**************
1)Manas Nayak
2)Alina Geeta
3)Carolina Rocha
4)Agnes Martinen
5)Praveen Kumar
6)Agnes Martinen
7)Alina Geeta
8)Sunith Singh
9)Charbak Roy
10)Rama Sri
****Program completed successfully....****

Negative Test Output:

Add a comment
Know the answer?
Add Answer to:
(c programming): write a program that contains a function named getName, that does not get any...
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
  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a C program to do the following: 1. Write a C function named find that...

    Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Write a Java program that reads a series of strings from a user until STOP is...

    Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either...

  • c programming

    Write a c program that reads a string with a maximum length of 30 from the keyboard[1]. The program should then copy the characters from that input string into a second character array (in order of input). However, only if a character is a vowel (a, e, i, o, u) should it be copied into the second array. Once copied, the program should output the values stored in the second array (in order of input). The program should then count...

  • Write a C++ program to get an input string from the user, and output the string...

    Write a C++ program to get an input string from the user, and output the string in uppercase letters. Use a dynamic character array to store the string. ?(this can be from any input file, there is no specific file.)

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