answersLogoWhite

0

#include<iostream>

#include<fstream>

#include<vector>

#include<string>

std::string get_input_file()

{

while (true)

{

std::cout << "Enter the input file name: ";

std::string filename;

std::getline (std::cin, filename);

std::ifstream file (filename);

if (file.is_open())

{

file.close();

return filename;

}

std::cerr << "The file could not be opened.\n" << std::endl;

}

}

std::string get_search_word()

{

while (true)

{

std::cout << "Enter the word to search for: ";

std::string search;

std::cin >> search;

if (search.size())

return search;

std::cerr << "The search word cannot be an empty string.\n" << std::endl;

}

}

std::vector<size_t> get_offsets(const std::string& filename, const std::string& search)

{

std::vector<size_t> offsets;

std::ifstream file (filename);

if (!file.is_open())

throw std::exception("The file could not be opened.");

size_t offset = 0;

while (!file.eof())

{

size_t pos = 0;

char c;

while (!file.read(&c, 1).eof() && c!=search[pos])

++offset;

while (pos!=search.size()-1 && !file.read(&c, 1).eof() && c==search[++pos]);

if (pos==search.size()-1 && c==search[pos])

offsets.push_back (offset);

offset += pos;

pos = 0;

}

file.close();

return offsets;

}

int main()

{

std::cout << "Word Search\n" << std::endl;

std::string filename = get_input_file();

std::string search = get_search_word();

std::vector<size_t> offsets = get_offsets (filename, search);

std::cout << "The word was found " << offsets.size() <<

" time" << (offsets.size()==1?"":"s") << ".\n" << std::endl;

if (offsets.size())

{

std::cout << "Offsets:\n";

for (auto off : offsets)

std::cout << off << std::endl;

}

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

Is it possible to write a C plus plus or java program to draw an activity flow diagram given the sequence as a text file input and store the diagram in an output file?

Yes, this is possible.


What c plus plus program must be saved in a text with the extension cpp?

The .cpp extension is merely conventional; it is not required by the C++ standard. You can actually use any file extension you wish.


Why do you write your code in a Notepad file?

Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.


What is the basic requirements to develop a c plus plus program?

Text editor.


Why would you need to convert a string to a numeric data type?

For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.

Related Questions

Is it possible to write a C plus plus or java program to draw an activity flow diagram given the sequence as a text file input and store the diagram in an output file?

Yes, this is possible.


What program can you use to save a file in the rich text format file format?

Wordpad, Notepad, any equivalent text editing program.


What is README file?

It's a help file that contains some text about the program it belongs to.


What c plus plus program must be saved in a text with the extension cpp?

The .cpp extension is merely conventional; it is not required by the C++ standard. You can actually use any file extension you wish.


Why do you write your code in a Notepad file?

Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.Notepad is a text editor. What it saves is the source code of the programming language. Any source code is just a text file, so Notepad or any text editor can save a program's code.


What is the basic requirements to develop a c plus plus program?

Text editor.


What program saves files as a CSS file?

Any text editor will, just save the file with .css at the end.


Is a cookie a computer program?

No, a cookie is a text file. A cookie can be read as a resource by a program but, on its own, it cannot execute.


A program in c to copy text file?

File Operations in C are accomplished by the use of pointers.Pointers are use to point to a particular memory location.File are read and written by using file pointers.The Keyword FILE is used to declare a file pointer.The Complete Program for writing text to a file and saving it and copying it to a new location is given at http://c-madeeasy.blogspot.com/2011/07/program-in-c-to-write-data-to-file-and.html


How do you Open a text file as C plus plus source code?

All C++ source code is is a text file with the .cpp extension. So if you save your code as *****.cpp then it is automatically C++ source code.


Why would you need to convert a string to a numeric data type?

For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.


What program can read php file?

PHP files are plain text, so you can read them with your text editor or IDE of choice.