Question

5.19 LAB: Contains the character Write a program that reads an integer, a list of words,...

5.19 LAB: Contains the character

Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list will always contain less than 20 words. Each word will always contain less than 10 characters and no spaces.

Ex: If the input is: 4 hello zoo sleep drizzle z

then the output is: zoo drizzle

To achieve the above, first read the list into an array. Keep in mind that the character 'a' is not equal to the character 'A'.

Hint: To read in the character after the final word, add a space before %c: scanf(" %c", &searchCharacter);

PLEASE ANSWER in c

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <stdio.h>

int main(void) {

int n, i, j;

char a[20][20], ch;

scanf("%d", &n);

for(i=0; i<n; i++)

scanf("%s", a[i]);

scanf(" %c", &ch);

for(i=0; i<n; i++)
{
for(j=0; a[i][j]; j++)
{
if(a[i][j] == ch)

{

printf("%s\n", a[i]);

break;

}
}
}
return 0;

}

Kindly revert for any queries

Thanks.

Add a comment
Answer #2

Here's a possible solution in C:

arduinoCopy code#include <stdio.h>int main() {    char words[20][11];  // maximum of 20 words with 10 characters each
    int n, i, j;    char searchCharacter;    // read number of words
    scanf("%d", &n);    // read words into array
    for (i = 0; i < n; i++) {        scanf("%s", words[i]);
    }    // read search character
    scanf(" %c", &searchCharacter);    // check each word for search character and print if found
    for (i = 0; i < n; i++) {        for (j = 0; words[i][j] != '\0'; j++) {            if (words[i][j] == searchCharacter) {                printf("%s\n", words[i]);                break;  // no need to check rest of word
            }
        }
    }    return 0;
}

Explanation:

  1. We declare a 2D character array words with dimensions 20 (maximum number of words) and 11 (maximum number of characters per word, including null terminator).

  2. We read in the number of words n using scanf.

  3. We use a loop to read in each word using scanf("%s", words[i]).

  4. We read in the search character using scanf(" %c", &searchCharacter), noting the space before %c to consume the newline character left in the input buffer after the last scanf.

  5. We use nested loops to check each word for the search character. The outer loop iterates over the words, and the inner loop iterates over the characters in each word. We stop searching a word as soon as we find the search character using break.

  6. If we find the search character in a word, we print the word using printf("%s\n", words[i]).

Example input/output:

makefile

Input:4 hello zoo sleep drizzle zOutput:zoo drizzle


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
5.19 LAB: Contains the character Write a program that reads an integer, a list of words,...
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
  • SOLVE IN C: 6.26 LAB: Contains the character Write a program that reads an integer, a...

    SOLVE IN C: 6.26 LAB: Contains the character Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list will always contain less than 20 words. Each word will always contain less...

  • C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list...

    C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Ex: If the input is: 4 hello zoo sleep drizzle z then the output is: 200 drizzle To...

  • 7.13 LAB: Word frequencies (lists) Write a program that first reads in the name of an...

    7.13 LAB: Word frequencies (lists) Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: input1.csv and the contents of input1.csv are: hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy the output is: hello 1 cat 2 man...

  • Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method.

     Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: inputl.csv and the contents of input1.csv are: hello, cat, man, hey, dog, boy, Hello, man, cat, woman, dog, Cat, hey, boy the output is: hello 1 cat 2 man...

  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

  • Creat a C Program that Reads words from a file called words, which contains one word...

    Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum length to M.The number of words in the file is equal to N. Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one. 1) When you finish reading the file,...

  • Python 3 Write a program that reads a sequence of words and prints every word whose...

    Python 3 Write a program that reads a sequence of words and prints every word whose frequency is equal to its length. You can assume that there will always be a word that meets this condition. If more than one word have this condition, you must print first the most frequent one. If two or more words with equal frequencies meet this condition, print them from the smallest to the largest in alphabetical order. Sample Input lee john lee peter...

  • Write a Java program that reads a word and prints its bigrams substrings. A character bigram...

    Write a Java program that reads a word and prints its bigrams substrings. A character bigram is defined as a continuous sequence of two characters in a word. For example, if the user provides the input "rum", the program prints ru um Hint: 1. set two pointers, one always moves from the beginning of the string and the other moves from i+2, in which i is the current position of the first pointer. 2. print an error message if the...

  • In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq)....

    In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter words to the function...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

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