C++ How can I compare a user input to a list of string with strict ordering?

nanjero05

I am trying to create a search array function where I have a list of string and I take a user input. I then compare the user input to the strings in the array and output any that matches any string in exact order of the input. I am not sure where to start. like so:

array: "this", "is", "a", "test"

user input: "t"

The output should then be "this" and "test"

This is what i have so far:

string arr[6] = {"hello", "this", "is", "a", "test", "string"};
vector<string> words;
for (int i=0; i<5; i++) {
    if (a.find(arr[i]) != string::npos) {
        words.push_back(arr[i]);
    }
}

for (int i=0; i<words.size(); i++) {
    cout << words[i] << endl;
}

at the moment it only searches for exact matches. How could i make it to the scenario described above?

ATul Singh

Change this

if (a.find(arr[i]) != string::npos) 

to

if (arr[i].find(a) != string::npos) 

You will search your User string in the List of strings one by one.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I compare user input to an uppercase letter string in a list?

Python: How can i compare a User-Input-String in an if-Statement?

How to compare user input with string?

How can I print a list by user input?

How can i link a user input to a string

How can I check if the user input is a string

How can I compare a string to a "filter" list in linq?

compare user input with a string

How can I display a list of numbers that start with a user input in C# and the .NET Framework?

How do I compare user input to a variable?

How do I replace a certain string in a list from user input?

How can i get the input from the user and display the sorted list

how can i add user input in to the empty string in def?

How can I compare a input to a list of methods then if input is equal to a method output that method

How can i compare and seach string and datetime c#

How can I compare a richtextbox to a string in WPF/c#

How can I retrieve three variables from a user input in C

How can I set User Input from Code in C++?

How do i sort mathematical vectors by strict weak ordering for a map?

How can I quickly compare a list and a set?

How can i compare two list of dictionaries

How to compare two separate types within a list using user input?

How can i split text input to a list in C++?

How to put string and integer in list by user input

How can i compare string and character types?

How to compare user input (from std::cin) to a string?

How to compare user input with string getting from process.stdin.on()?

How can I remove 'garbage input' from a C string?

How can I allow a user to input individual list items into a list of lists?