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
  • 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...

  • 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...

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

  • 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....

  • Write a program that reads a series of words (one word per line) from a file...

    Write a program that reads a series of words (one word per line) from a file named data.txt. Each word in the file should have each of its characters shifted by 1 character value in the ASCII table (incremented) and then that new word with its characters shifted should be printed to a new file named result.txt. Each word from data.txt should be reprinted with its new encoding in result.txt. Your program should not have any knowledge of how many...

  • Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter charact...

    Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter character • Contains at least 1 upper letter character • Contains at least 1 numeric digit • Contains at least 1 special character from the set: !@#$%^&* • Does not contain the word “and” or the word “the” Prompts the user for a password, including the requirements above in your output. Output a string that states valid or invalid. If invalid, state which...

  • 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...

  • 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 C++ program that reads in a text file and reads each character or lexemes...

    write a C++ program that reads in a text file and reads each character or lexemes and states what token it is and writes to another file for example: while (fahr < upper) a = 23.00 whileend Output: token lexeme -------------------------------- keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 keyword whileend the program aslo reads in comments but does not add to the list. characters that are commented out or...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

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