finding a word in a string

Hi all, iam new at c++ and I need some help. if I have

void main(){
char x[20]={"this is a test"},y[20];
cout<<"enter the word you want to check";
cin>>y;

so if I enter "test" it will do the following
cout<<"the word"<<y<<"is found in the array";

so what should i use to check if the word is found ???
You may want to use strings instead of char arrays. They are a lot more reliable since they are dynamic. Visit these pages:
http://xoax.net/comp/cpp/console/Lesson46.php
http://www.cplusplus.com/reference/string/
http://www.cplusplus.com/reference/string/string/find/

Note:
The xoax.net tutorial may sound like a lot of mumbo-jumbo at the start, but it's not hard to follow after about 1:30 minutes.
Last edited on
you havent stated the problem clearly but i think that you want the user to input any word, so that you can search the array x, to see if it contains the word.. rite..?

lets see,
1. get the y[0] and check with the x[i]
2. if you find it, do the following
i. check the y[1] with x[i+1] and if it matches check the next untill you come to y[j] == NULL
3. go to next x ( i++ ) and repeat the process....
thank you all my problem has been solved :D:D
Since this was solved I see no problem in pasting my solution to the thread.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// String (Char *) search.

#include <iostream>
#include <cstring>

using namespace std;

bool isInCharString (char *str1, char *search);

int main ()
{
  if (isInCharString("The Quick Brown Fox Jumped Over The Lazy Dog!", "Lazy"))
    cout << "It is here!" << endl;
  return 0;
}

bool isInCharString (char *str1, char *search)
{
  for (int i = 0; i < strlen(str1); ++i)
  {
    if (strncmp (&str1[i], search, strlen(search)) == 0)
      return true;
  }

  return false;
}
Topic archived. No new replies allowed.