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.
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//FUNCTION PROTOTYPE AND DEFINITION OF THE 'findstring' FUNCTION
void findstring(char *scanptr, constint 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()
{constint 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;
elsegoto top;
}
#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 (unsignedint i = 0; i < strlen(input) ; ++i) // Use locally scoped int here
{//POINTER TO POINTER COMPARISON
if ( input[i] == wordptr[0] )
{
for(unsignedint 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
constint 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;
}
// word scan.cpp : main project file.
//Include files
#include "stdafx.h"
#include<iostream>
/* yum yum namespaces */
usingnamespace System;
usingnamespace 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;
}