Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ getting "context" around a keyword in a string
New Posts  All Forums:Forum Nav:

C++ getting "context" around a keyword in a string

post #1 of 2
Thread Starter 
Hi,

I'm having a lot of trouble with this one.

Problem:
I am given a string that is in the form of song lyrics. I need to find a keyword in the string, e.g. the word "be". Once found, I need to get the words preceding, including, and following the keyword (this is my context).

I have been using strtok to grab individual words, but this doesn't seem to be a viable solution to this problem since it's somewhat destructive (replaces whitespace with null terminators).

An additional problem is that the keyword may be in the string multiple times, and I must do the same for every single occurence.

Is there a function that will let me go through the string, looking for the word and return its location without being destructive?

Also, I'm having a problem where whitespace in a string is read as a null terminator when copied into a cstring. (I believe this is the problem: I get strange characters printing out where whitespace should be.)

Thanks everyone.
Current
(12 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Z68 Extreme4 Gen3 EVGA GTX 570 Classified 4x4GB Corsair XMS3 
Hard DriveCoolingPowerCase
Samsung 830 Thermaltake Frio OCZ ZS 850W Lian Li Lancool First Knight K9 
MouseAudioOtherOther
Logitech G500 Audinst HUD-MX1 Sennheiser HD598 Swan D1080MkII 
  hide details  
Reply
Current
(12 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Z68 Extreme4 Gen3 EVGA GTX 570 Classified 4x4GB Corsair XMS3 
Hard DriveCoolingPowerCase
Samsung 830 Thermaltake Frio OCZ ZS 850W Lian Li Lancool First Knight K9 
MouseAudioOtherOther
Logitech G500 Audinst HUD-MX1 Sennheiser HD598 Swan D1080MkII 
  hide details  
Reply
post #2 of 2
Sounds to me like you need to use a vector that holds three strings. Then run through your lyrics and put the individual words into the vector. Check the middle index of the vector to see if it matches your keyword, and if it does, store that vector in another vector. Then when you've gone through your whole string, return the vector of vectors.

For example:
Lyrics "These are the lyrics to the most awesome song ever"
Keyword: the
1st loop: Vector = {These, are, the} ; Middle element != keyword
2nd loop: Vector = {are, the, lyrics} ; Middle element = keyword ; Store vector into vector of vectors
repeat...

As for your whitespace problems, I've never used strtok, but it seems to me stringstreams would get rid of your problem.
You'll end up with a vector that stores 2 vectors, {are, the, lyrics} and {to, the, most}
Edited by superhead91 - 4/26/11 at 10:18am
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ getting "context" around a keyword in a string