Register and join over 40,000 other developers!
Recent Topics
-
Las Vegas Phone Services [hostedpbx ip phone]
NocRoom - Jan 25 2019 05:20 AM
-
Building a web app similar to Google sheets
Bissy - Dec 29 2018 05:31 AM
-
How would make this tutorial code print points incorrect order
sonar87 - Nov 03 2018 07:42 PM
-
set property with HTML content as string in from include not possible
LDDbyD - Oct 01 2018 06:33 AM
-
C++ FPS Game Development using Dark GDK
DaDopeman - Aug 12 2018 07:36 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- import
- syntax
- hardware
- asp.net
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- programming
- javascript
- android
- css
- assembly
- form
- c
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- setup
- sql
- help
- python
- combobox
- binary
- game
- 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++ →
basic c++ question, difference between a and a&Started by Siten0308, 10 Oct 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
Trying to makes sense of unnamed namespace and extern useStarted by sonar87, 25 Jan 2017 ![]() |
|
![]() |
||
Language Forums →
PHP →
How do I search Google anonymously by directly typing the keyword into Url ?Started by uniqueideaman, 21 Jan 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
I am new at programmingStarted by Twitty, 18 Jan 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
How to implement password access to specific file type in Linux/Windows programmatically?Started by Kosarar, 08 Jan 2017 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download