Search Arrays in Text File

Alrite, I need to search for a word given by a user, my problem is how do you
go about searching the whole array to find the particular word.
P.S - using text files and have initialised the characters into an array of size 1000.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
    int MemIndex = 0, count = 0;
    const int Num = 100;
    char Array[Num];
    string line,FindWord, ReplaceWord, fileData;
    int SizeWord; //= FindWord.length();
	int SizeReplace = ReplaceWord.length();
    
    cout<<"Enter word to look for:";
    cin>>FindWord;
    SizeWord = FindWord.length();
    
   ifstream InFile ("Text.txt");  
   if (InFile.is_open())
  {
    while ( InFile.good() )
    {
      fileData += InFile.get();    
      getline (InFile,line); 
      cout << line << endl;
      //cout<<line.length();
      cout<<line.size();
      for(int i = 0; i < line.length(); i++)
      {       count = 0;
          Array[MemIndex] = line[i]; //copy each line read to the main memory ;
          //cout<<Array[MemIndex]; 
          for(int j = 0; j < SizeWord; j++)
          {
                  Array[MemIndex] = line[j];
                  cout<<Array[MemIndex];
                  
           if(Array[i+j] == SizeWord[j]){
           count ++;
           //Lost Here               
           }
                 
          }
          MemIndex++; //increment memory position
      }
  
    
    } 
}

Thanks.
I think your question should read: "I need to replace all instances of a word in a file."

Am I right?
I don't really know, all
I want, is to search the array of characters for the particular word
Thanks....
Okay, so, you are giving us an array of characters and a word. What exactly do you want? Do you want the index of the first character of the first instance of the word in the array? What should the function return if no match is found? Be more specific.
Im really sorry,
And yes I want the index of the character that is being searched for, and tell the user that it exists if it doesn't exist just cout a statement that says, it doesn't exist.. The search should start form the beginning of the sentence and go through each letter, searching whether its the same with the given word or not. The sentence is just one line.

Thanks
Try this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int find(char sentence[], std::string word)
{
    int j = 0;
    
    for (int i = 0; sentence[i]; i++)
    {
        if (sentence[i] == word[j])
            j++;
        else
            j = 0;

        if (j == word.length())
            return i - j + 1;
    }

    return -1;
}

You need to pass it a null-terminated character array and a string. It returns the index of the first character of the first instance of the string in the array. If there is no match, it returns -1.
Thanks, Josue,
What if it doesn't require functions, then how would it look like.
Thanks
Topic archived. No new replies allowed.