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

Respuesta :

Solution and Explanation:

// code

#include <iostream>

#include <vector>

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

}

Following are the progrm to matched the input value.

Program Explanation:

  • Include header file.
  • Defining a main-method.
  • Inside the method three integer variable "n, i, j", one array of string "l" , one string variable "s", and one character (char) variable "c" is defined.
  • In the next step, "n,l, s, and c" is used to input value, and define a for loop is defined that use if block to check string value contain the char value , and prints its value.

Program:

#include <iostream>//header file

using namespace std;

int main()//main method

{    

int n,i,j;  //defining integer variable    

string l[n];//defining an array of string    

string s;//defining a string variable    

char c;//defining a char variable    

cin>>n;//input n value            

for(i = 0;i<n;i++)//defining loop to input string value    

{    

    cin>>l[i];//input string value    

}  

  cin>>c;//input char value    

//defining loop that check string value contain input character value  

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

  {  

     for(j = 0;j<l[i].length();j++)//defining loop that check string value  

     {            

if(l[i][j]==ch)//using if block that check string value equal to character value             {              

  cout<<l[i]<<endl;//print matched value                

break;//break the condition      

     }    

   }  

 }  

 return 0;

}        

Output:

Please find the attached file.

Learn more:

brainly.com/question/13543413

Ver imagen codiepienagoya