Ask Question
23 August, 17:27

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);

+3
Answers (1)
  1. 23 August, 18:13
    0
    Solution and Explanation:

    / / code

    #include

    #include

    using namespace std;

    / / this function returns true if given character is in given word

    bool has_char (string temp, char c)

    {

    for (int i = 0; i < temp. length (); i++)

    if (temp[i] = = c)

    return true;

    return false;

    }

    int main ()

    {

    int n;

    vector words (n);

    char character;

    / / read integer

    cin >> n;

    / / read list of words

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

    cin >> words[i];

    / / read a character

    cin >> character;

    / / print those words which contains given character

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

    {

    if (has_char (words[i], character))

    cout << words[i] << " ";

    }

    cout << endl;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers