Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

How To Find All The Same Strings C++
Started by jasonalien, Jul 06 2012 03:50 AM
file c++ search find string
8 replies to this topic
#1
Posted 06 July 2012 - 03:50 AM
Hello,
I need to find all of the same words in a text file. For example I need to find all "apples" in that file. After that I need to take the string which is next to the apple. I would appreciate any help, especially for the first part.
I need to find all of the same words in a text file. For example I need to find all "apples" in that file. After that I need to take the string which is next to the apple. I would appreciate any help, especially for the first part.
#2
Posted 06 July 2012 - 05:45 AM
Scan character by character for the letter 'a'. Once found, check the next letter to see if it's a 'p'. If it is, check the next letter to see if it's a 'p'. If it is, check the next letter to see if it's an 'l'. And the next letter if it's an 'e'. And the next if if it's an 's'. If you successfully match each of these letters, you've matched the word "apples", so store the occurrence and reset for the next character and resume. If at any point during the middle of the word, if the next letter doesn't match the expected letter, drop out of the match, reset, and continue with comparing 'a' with the next character.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#3
Posted 06 July 2012 - 06:00 AM
Thanks for the answer
How will I iterate in the text file? How will I store the occurence?
How will I iterate in the text file? How will I store the occurence?
#4
Posted 06 July 2012 - 06:26 AM
How will I iterate in the text file?
http://cplusplus.com...am/istream/get/
Use istream::get() to grab one character at a time. Place this in a while loop testing ios::good() to see if you can keep reading characters from the file.
How will I store the occurence?
Keep a counter of how many times the search word appears. Increment this on a successful match (i.e., when you match the last letter in the word.) Keep a state variable to tell where you are in the word as you are matching it.
Also, store the position in the file where the word occurs, if this is important. You'll need an array of integers for this. Index 0 will store the byte location of the first occurrence, index 1 will be the location of the 2nd occurrence, etc.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#5
Posted 06 July 2012 - 08:18 AM
are you familiar with regular expressions library ? that will make it easy for you, capturing in groups of previous word - apple - next word, giving you a collection you can iterate ...
#6
Posted 06 July 2012 - 08:55 AM
If you decide to go with BlackRabbit's suggestion, there are several regular expression libraries for C/C++. Two that I know of are PCRE (http://pcre.org/) and Boost (http://www.boost.org...html/index.html). Regex will certainly be a much quicker solution, however, if the purpose is to learn how to perform these matches algorithmically on your own, regex won't give you that.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#7
Posted 06 July 2012 - 06:58 PM
It seems you are a beginner? If yes, I think you better follow gregwarner's first. Then when you will know how it is working, you can go for BlackRabbit's suggestion for REGEX way (using regular expression libraries) to know about it.
#8
Posted 06 July 2012 - 11:10 PM
I'm a "very" beginner. Thanks for helping..
#9
Posted 04 August 2012 - 05:30 AM
It's a bit nonsensical to describe string-finding algorithms as O(N) or O(N*N) when there are really two parameters that determine the runtime, N_haystack and N_needle. Pretty much any algorithm is O(N_haystack) when N_needle = 1. Most are O(N_haystack * N_needle) or better, and you may be able to assume N_needle <= C
dapfor.com/en/net-suite/net-grid/tutorial/threadsafe-bindinglist
dapfor.com/en/net-suite/net-grid/tutorial/threadsafe-bindinglist
Also tagged with one or more of these keywords: file, c++, search, find, string
Language Forums →
C and C++ →
Do you use any debuggers except the standard for Visual Studio?Started by Fernando, 25 Jan 2019 ![]() |
|
![]() |
||
Tutorial Forums →
C/C++ Tutorials →
Basic C++ Game Hacking TutorialStarted by DaDopeman, 12 Aug 2018 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
basic c++ question, difference between a and a&Started by Siten0308, 10 Oct 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
Why is similar string comparison giving different result?Started by nick112, 29 May 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
help me . c++. ArraysStarted by girl2383, 06 Feb 2017 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download