473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

word searching program (but it is not searching for some words).

#include <iostream> // library that contain basic input/output functions
#include <fstream> // library that contains file input/output functions
using namespace std;

int main()

{
int ocurrences_count = 0;
char word[20]; //this array will save user input
int array_size = 1024; // define the size of character array
char * array = new char[array_size]; // allocating an array of 1kb
int position = 0; //this will be used incremently to fill characters in the array

//prompting user to enter a word to be search in the file
cout << "Please enter the word to search in file : ";
cin.getline(word,19); //reading user input of max 19 characters because our word array size in 20
int word_size = 0;
//this loop is calculating the length of input word
for(int i = 0; word[i] != '\0'; i++)
{
word_size++;
}

ifstream fin("test.txt"); //opening an input stream for file test.txt
/*checking whether file could be opened or not. If file does not exist or don't have read permissions, file
stream could not be opened.*/
if(fin.is_open())
{
//file opened successfully so we are here
cout << "File Opened successfully!!!. Reading data from file into array" << endl;
//this loop run until end of file (eof) does not occur
while(!fin.eof() && position < array_size)
{
fin.get(array[position]); //reading one character from file to array
position++;
}
array[position-1] = '\0'; //placing character array terminating character

//this loop is searching for the word in the array
for(int i = 0; array[i] != '\0'; i++)
{
for(int j = 0; word[j] != '\0' && j < 20 ; j++)
{
if(array[i] != word[j])
{
break;
}
else
{
i++;
if(word[j+1] == '\0')
{

cout << "Word Found in File at position " << (i-word_size) << endl;
ocurrences_count++;

}
}
}
}
cout << "Total occurences found : " << ocurrences_count << endl;
}
else //file could not be opened
{
cout << "File could not be opened." << endl;
}
return 0;
}
Jun 9 '14 #1
1 1284
weaknessforcats
9,208 Expert Mod 8TB
When searching for a word by reading a character at a time, you have to skip any leading whitespace characters. The word will start at the first non-whitespace character.

The word will end when the character read is a)a whitespace character, or b) a \0. In both cases you would add a \0 to the array containing the word.

I don't see logic like this in the code.
Jun 9 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Isaac Gouy | last post by:
Seems like there's a problem with the word-frequency program written for "The Great Computer Language Shootout" - it's using 161,500KB which is far more than other programs: ...
8
by: PHP2 | last post by:
how create small program that push some button automaticaly (every 9 mins) for Linux with C++? any fast advices?
11
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h>...
0
by: Jammy | last post by:
now I have a program, it need to check some files which have some key word in it, but I don't know how to do it. I have try to use the filestream to open the file , but the file format...
5
by: Jintty | last post by:
Hi, I'm trying to write a program that will read a txt file, copy it into another text file and display the number of words, lines and paragraphs. I was able to get the copying portion done, but...
1
by: junglenut | last post by:
please give me a reverse word java program in a form of jcreator javax.swing.*; source code.. because i really have a hard time figuring it out on how to make it.. please give me a reverse word...
16
by: feda | last post by:
Hi everybody, I am new to java I installed netbeans 6.0.1 I tried running the following simple program but it didnor work??? import java.util.*; public class Hello { public...
11
by: KillSwitch | last post by:
Is it possible to make a program to search a site on the internet, then get certain information from the web pages that match and display them? Like, you would put in keywords to be searched for on...
1
by: ztealmax | last post by:
Trying to compile but get some errors (Im really new at this) :-) Hello im having hard time compiling this could anyone that knows what they are doing see what is wrong with this code? The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.