Write a C++ program that erase the nonalphabetical string from the vector.Ask us
ID: 3731816 • Letter: W
Question
Write a C++ program that erase the nonalphabetical string from the vector.Ask user to enter 3 string.
Example: string is: Apple,Cat,Ship,Bat,Car,Cry
Use push_back to add that in vector.
UserInput(How many char word want): 3
then delete Apple,Ship from the vector
Loop until user find correct word
UserInput(Guess a character from the word):r
Display:Word found: _ _ _
then delete the word of lenght 3 that has r in it
UserInput(Guess a character from the word):a
Display:Word found: _ a _
Now all the remaining word of lenght 3 has character a at same place
UserInput(Guess a character from the word):t
remove all the remaining word that has character t (cat, bat)
Display:Word found: _ a _
UserInput(Guess a character from the word):r
Display:Word found: _ a r
UserInput(Guess a character from the word):c
Display:Word found: c a r
Congratulations you win!!
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
vector <string> v;
string str;
int a;
while(1) {
cout<<"Wants to enter String(Enter 1 else 0)"<<endl;
cin>>a;
if(a==1) {
cin>>str;
v.push_back(str);
}
else
break;
}
int len;
cout<<"How many char wants you in a word"<<endl;
cin>>len;
for(vector<string>::iterator it = v.begin(); it != v.end(); ) {
if((*it).length()!=len) {
it=v.erase(it);
}
else
it++;
}
cout<<"Remaining Strings"<<endl;
for(vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
cout<<*it<<endl;
}
char ch;
while(1) {
if(v.size()!=1) {
cout<<"Guess a character from the word"<<endl;
char arr[100][100];
int idx=0;
cin>>ch;
int min = INT_MAX,index = INT_MAX,flag = 0,f=0;
for(vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
string s = *it;
f=0;
for(int i=0;i<s.length();i++) {
if(s[i]==ch) {
index = i;
f=1;
break;
}
}
if(f==1 && min==index && min!=INT_MAX) {
flag++;
strcpy(arr[idx], s.c_str());
idx++;
}
if(min>index) {
flag = 1;
idx=0;
min = index;
strcpy(arr[idx], s.c_str());
idx++;
}
}
if(idx>0) {
cout<<"Word Found"<<endl;
for(int j=0; j<idx; j++) {
cout<<arr[j]<<endl;
}
}
if(flag!=v.size()) {
for(int j=0; j<idx; j++) {
for(vector<string>::iterator it = v.begin(); it != v.end();) {
if(arr[j]==*it) {
it=v.erase(it);
break;
}
else
it++;
}
}
}
}
else
break;
}
for(vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
cout<<*it<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.