Find a word in given string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Find a word in given string

there is a string array and we have to find a word from that string array ..

2nd Jan 2017, 2:56 PM
Mohd Sabahat
Mohd Sabahat - avatar
5 Answers
+ 6
Let's say you have string array: string array[] = {...}; To search through the array, we need a specific keyword as the search term: cin >> specific_key; Then we can compare the specific search term to all the strings in the array: for (int i = 0; i < array.length(); i++) { if (specific_key == array[i]) { cout << "Found : " << array[i] << " at index position " << i << endl; } }
2nd Jan 2017, 3:29 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
The specific key provided in my example is also a string.
4th Jan 2017, 11:05 AM
Hatsy Rei
Hatsy Rei - avatar
0
int search(string* array, int size) { for (int i = 0; i < size; i++) { //compare is a string function that compares the strings, returning 0 if they are equal if (array[i].compare(word) == 0) return i } return -1 //if the word wasnt found, return -1 }
2nd Jan 2017, 3:37 PM
Alexsandro Vítor
Alexsandro Vítor - avatar
0
but the specific key is not a character it's a string...
4th Jan 2017, 11:04 AM
Mohd Sabahat
Mohd Sabahat - avatar
0
sa bende c++ üzerinde çalısıyorum tecrübeliysen bana yardımcı olurmusun telefon no :0545 587 29 30
28th Jan 2017, 7:51 AM
Ömer Yenitürk
Ömer Yenitürk - avatar