Scan Input to find PATTERN HELP!!!

Hello to all the programmers of the internets. I am in need of you all's assistance. I have a program that is suppose to take in a character string and scan the string to find a specific word. When it finds the word it needs to report that it found the word and where. I'm lost because it compiles properly but doesn't work. Any Assistance would be greatly appreciated. I'm respect the coder soon I will be one not just a Technophile. Code is below. I am using pointers and functions so you may need to know that syntax and how it works. Simple for most except me obviously.

http://blog.bit-logical.net

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
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

//FUNCTION PROTOTYPE AND DEFINITION OF THE 'findstring' FUNCTION
void findstring(char *scanptr, const int nelement)
//FUNCTION LOCAL VARIABLES
{ int charnum, count, compare ; char word[20];char *wordptr;

	cout << "What word are you looking for?? ";
	cin >>word;
	wordptr = word;
	//COUNTS THE NUMBER OF CHARACTERS OF THE WORD TO BE SEARCHED FOR IN THIS CARE 'PATTERN'
	for(charnum = 0 ; word[charnum] != '\n' ; charnum++);
	
	//THIS IS WHERE THE TRUE ANALYSIS WILL TAKE PLACE
	for (count = 0 ; count < nelement ; count++)
	{//POINTER TO POINTER COMPARISON
		if (( scanptr + count ) == wordptr )
			{
			for( compare = 0 ; compare < charnum; compare++ )
				if((scanptr + count + compare) != (wordptr +compare))
					if( compare == (charnum - 1))
						{
						cout << " I have found the word " << word << " from position ";
						cout << count << " to position " << count + charnum << " of your input."<< endl;
						}
			}
	}		
} 

int main()
{const int size = 1000; char inputarray[1000];
//VARIABLE FOR SWITCH
char control[1];
top:
     cout << "Go ahead and input something, I dare you: " << endl;
cin.getline(inputarray, size, '\n');
findstring(inputarray, size);
//SINCE APPLICATION CLOSES PREMATURELY I AM CREATING A USER CONTROLLED SWITCH FOR TERMINATION
        cout << "Do you want to quit or try again? Press [y/n] and Enter";
        cin >> control;
    if(control == "y")
           return 0;
    else
        goto top;
}

Hmm I have fixed this. However the code still needs a bit of work to ensure you don't get buffer overflows etc.

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
52
53
54
55
56
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

//FUNCTION PROTOTYPE AND DEFINITION OF THE 'findstring' FUNCTION
void findstring(char input[])
{ 
  //Variables - Formatted nicer
  int charnum;
  char word[20]   = {0}; // Init to null.
  char *wordptr;

  cout << "What word are you looking for?? ";
  cin >>word;
  wordptr = word;
  
  charnum = strlen(word); // Cleaner. How long is Word
  cout << "Word: " << word << " is " << charnum << " chars long" << endl; // Debug 
  
  //THIS IS WHERE THE TRUE ANALYSIS WILL TAKE PLACE
  for (unsigned int i = 0; i < strlen(input) ; ++i) // Use locally scoped int here
  {//POINTER TO POINTER COMPARISON
    if ( input[i] == wordptr[0] )
    {
      for(unsigned int j = 0 ; j < strlen(wordptr); ++j ) // Locally scoped
      {       
        if(input[i+j] == wordptr[j]) {
          if( j == strlen(wordptr)-1)
          {
            cout << " I have found the word " << word << " from position ";
            cout << i << " to position " << ((i+j)+1) << " of your input."<< endl;
          }
        }
      }
    } // Proper Indentation
  }   
} 

int main()
{
  // Seperate these by line, makes it easier to read
  const int size = 1000; 
  char inputarray[1000]     = {0}; // Init to nulls

  // don't use Labels and gotos. Thats BAD BAD practice.
  // Find another way to do it.
  cout << "Go ahead and input something, I dare you: " << endl << "> "; // Gave them a prompt.
  cin.getline(inputarray, size, '\n');
  cout << "You want to search through: " << inputarray << endl; // Debug
  findstring(inputarray);

  system("PAUSE"); // Ask user to press any key to continue
  return 0;
}
sorry for bad commenting in the code. its pretty simple. it also works for me when i compile.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// word scan.cpp : main project file.


//Include files
#include "stdafx.h"
#include<iostream>

/* yum yum namespaces */
using namespace System;
using namespace std;


/* this is where we like to scan the string :] */
void scanString(char input[])
{
	/* initiate some variables */
	int count;
	int wcount = 0;
	int wordlen = 0;
	char word[32];

	/* this finds the length of the string, because you know that after reading the line, it is terminated with a '\0' */
	int size = 0;
	while(input[size] != '\0') {
		size++;
	}
	/* add one more to the size since it does not add one for the terminating thing */
	size++;
	
	/* ask for the word */
	cout << "\n\nWhat word are you looking for? ";
	cin >> word;
	
	/* gets teh word length, same idea as the string length */
	while(word[wordlen] != '\0') {
		wordlen++;
	}
	
	/* now time to scan the string until the end */
	for(count = 0; count < size; count++) {
		/* if the current char is equal to what ever letter of the word your looking for we are on is equal */
		if(input[count] == word[wcount]) {
			/* adds one to word letter count */
			wcount++;
			/* if we have found all the letter in order, tell teh user */
			if(wcount == wordlen) {
				cout << "\n\nFound the word you were looking for, \"" << word << "\" at the position between " << (count - wordlen) + 1 << " and " << count << ".\n\n";
				wcount = 0;
			} 
		} else {
			/* if the current chars are not equal, make sure we are lookign for the first letter of the word */
			wcount = 0;
		}
	}
}




int main()
{
	char string[1024];
	cout << "Enter a string...\n\n";
	cin.getline(string,1024);
	scanString(string);

	return 0;
}

Hello, I'm not sure if you're asking for help or giving a progress report :-).

guestgulkan: I agree :P I posted a solution, but Oh well.
just posting how i programmed this little task. tis all.
Topic archived. No new replies allowed.