53.3k views
1 vote
5.21 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);

2 Answers

4 votes

Final answer:

To solve this problem, you can read an integer, a list of words, and a character. Then, iterate through each word and check if the character is present in the word. If the character is found, print the word.

Step-by-step explanation:

To solve this problem, you can follow these steps:

  1. Read the integer that represents the number of words in the list.
  2. Read the list of words into an array.
  3. Read the character that needs to be searched.
  4. Iterate through each word in the array and check if the character is present in the word.
  5. If the character is found in the word, print the word.

Here's an example code that implements this logic:

#include <stdio.h>

int main() {
int numWords;
scanf("%d", &numWords);

char words[20][10];
for (int i = 0; i < numWords; i++) {
scanf("%s", words[i]);
}

char searchCharacter;
scanf(" %c", &searchCharacter);

for (int i = 0; i < numWords; i++) {
if (strchr(words[i], searchCharacter)) {
printf("%s\\", words[i]);
}
}

return 0;
}
User Kunal Gupta
by
5.4k points
1 vote

Final answer:

To solve this task, read the integer representing the number of words, then read each word into an array. Iterate through the array and check if each word contains the given character using the 'strchr()' function. Print the words that contain the character.

Step-by-step explanation:

To solve this task, you can follow these steps:

  1. Read the integer that represents the number of words in the list.
  2. Use a loop to read each word into an array.
  3. Read the character using the format specifier " %c" (with a space before %c) to handle the newline character from the previous input.
  4. Use another loop to iterate through the array of words.
  5. Check if each word contains the character at least once by using the 'strchr()' function from the 'string.h' library. If the character is found, print the word.

Here's an example implementation:

#include <stdio.h>
#include <string.h>

int main() {
int numWords;
scanf("%d", &numWords);

char words[20][10];
for (int i = 0; i < numWords; i++) {
scanf("%s", words[i]);
}

char searchCharacter;
scanf(" %c", &searchCharacter);

for (int i = 0; i < numWords; i++) {
if (strchr(words[i], searchCharacter) != NULL) {
printf("%s\\", words[i]);
}
}

return 0;
}

User Spyros K
by
6.0k points