Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Find All The Same Strings C++

file c++ search find string

  • Please log in to reply
8 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

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.
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

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.
  • 0

ti-99-sig.png
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 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

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?
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

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.
  • 0

ti-99-sig.png
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 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

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 ...
  • 0

#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

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.
  • 0

ti-99-sig.png
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 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

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.
  • 0

#8 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 06 July 2012 - 11:10 PM

I'm a "very" beginner. Thanks for helping..
  • 0

#9 Avrillavigne

Avrillavigne

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

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
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download