search array of strings

the function should search the array of strings to find how many times the word 'the' was used. but its only returning a 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int FindThe (int The, char Paragraph[], char Pattern[])
{


  for (int i(0); i < 500; i++)
  {
     if (Paragraph[i] == Pattern[0])
     {
       for(int x(0); (x <= 3)&&(Pattern[x]== Paragraph[i+x]); x++)
       {
         if(x == 3 )
         The++;
       }
    }

  }
    return The;


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int x = 0; (x < int(strlen(Pattern))); x++)
       {
         if(Paragraph[i][x] != Pattern[x] )//Ignore this part, what you did here is right
         The = 0;
         else
         The++;
       }

//In main...
int main()
{
   int The = 0;

   if (FindThe(int The, char Paragraph[], char Pattern[]) == int(strlen(Pattern))
        cout << "a counter variable goes here..."<<
}
Last edited on
i cant do
if(Paragraph[i][x])
i and x are two different data types

edit: nvm. confused thought
Last edited on
ive implemented the loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int FindThe (int The, char Paragraph[], char Pattern[])
{
  int i(0);

  for(int i(0); i < 500; i ++)
  {
    if (Paragraph[i] == Pattern[0])
   {
       for(int x = 0; (x < (strlen(Pattern))); x++)
          {
            if(Paragraph[i+x] == Pattern[x] )
              The++;

         }

   }
   return The;
  }


}


and my main looks like this:
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
void GetParagraph (char Paragraph[]);
void GetData (int &NumLetters, int &NumWhite, int &NumWords, char Paragraph[]);
int FindThe (int The, char Paragraph[], char pattern[]);

int main()
{
  char Pattern[4] = {'t','h','e'};
  char Paragraph[500];
  int NumLetters(0);
  int NumWhite(0);
  int NumWords(0);
  int The(0);
  int count(0);

  GetParagraph(Paragraph);

  GetData(NumLetters,NumWhite, NumWords, Paragraph);
  cout <<"There are " << NumLetters << " Total letters and " << NumWhite + 1  << " total words" <<  endl;

  if ( FindThe (The, Paragraph, Pattern) == ( strlen ( Pattern ) ) )
  cout << "'The' occurs " << The  << " times " << endl;


   return 0;
}


but im still getting 0 whenever i run it
Last edited on
Line 5 of your FindThe function:

for(int i(0); i < 500; i ++)
Should instead be : for(int i(0); i < (int)strlen(Paragraph); i ++)


Line 7 of main()

char Pattern[4] = {'t','h','e'}; You have actually made a 2d array. Maybe this is what you want instead?

char Pattern[4] = "the";

Line 20:
if ( FindThe (The, Paragraph, Pattern) == ( strlen ( Pattern ) ) )
Since your function is returning a type int, cast the strlen() to int
if ( FindThe (The, Paragraph, Pattern) == int( strlen ( Pattern ) ) )


Here is what I have for you and it works:

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
#include <iostream>

using namespace std;

int FindThe (int The, char Paragraph[], char Pattern[])
{
    int toRet = 0;
    for(int i = 0; i < int(strlen(Paragraph)); i ++)
    {
        The = 0;        
        if (Paragraph[i] == Pattern[0])
        {
            for(int x = 0; (x < int(strlen(Pattern))); x++)
            {
                if(Paragraph[i+x] == Pattern[x] )
                    The++;
                else
                    The = 0;
                
                if (The == int (strlen(Pattern)))
                    toRet++;
                
            }

        }
    }
    return toRet;
    
}


int main()
{
    char Pattern[] = "the";
    char Paragraph[] = "There once was a man who was the king's greatest advisor and this man came to be known for his wisdom and kindness.";
    //int NumLetters(0);
    //int NumWhite(0);
    //int NumWords(0);
    int The = FindThe (The, Paragraph, Pattern);
    //int count(0);
    
        
    //cout <<"There are " << NumLetters << " Total letters and " << NumWhite + 1  << " total words" <<  endl;
    
    cout << "'The' occurs " << The  << " times " << endl;
    
    
    return 0;
}
thank you! this works
Topic archived. No new replies allowed.