Thread: Finding a word in a string.

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794

    Finding a word in a string.

    Basically looking for an easy way to do this for example:-


    I have a string such as below, and I want to find out if it contains
    the string "won" (I need to know who won the game).
    "Seat 4: My_poker_id (big blind) showed [Jh Ad] and won (3672) with Two Pairs, Eights and Fives, Ace high"

    If there an easy way to do this?

    To simplify it a little you can ignore the first part of the string and just use


    "(big blind) showed [Jh Ad] and won (3672) with Two Pairs, Eights and Fives, Ace high"

    points to note - the (big blind) bit might not be there, and "won" might not be
    there at all and the strings might be as below.
    As an example I would probably looking through the lines to find out who won.



    Seat 1: playername1 (big blind) folded before Flop (didn't bet)
    Seat 2: another layer folded on the Flop
    Seat 3: anotherplayer3 collected 112
    Seat 4: My_poker_id (big blind) showed [Jh Ad] and won (3672) with Two pairs, Eights and Fives, Ace high
    Seat 5: yetanother (button) folded before Flop (didn't bet)
    Seat 6: amdanother (small blind) folded before Flop (didn't bet)


    I don't think there is an easy was to so it with sscanf?
    Is there something else I should be using, or should I just search for 'w' then
    'o' then 'n'.I guesss that would be easiest.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How about strstr()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks
    How about strstr()?
    May be that will work, probably will actually buit I already have done this in the meantime.


    (the string being searched is in var2.) Should I use EOF or EOL or \0 or what?
    Code:
    int a1;
    for(a1=0; var2[a1]!=EOF; a1++)
    {
    		if(var2[a1]=='w' {
    		if (strcmp(&var2[a1],"won" ==0) {
    			printf("\n FOund it!! %s won, player");	
    			break;
    		}
    						
    	}
    				
    }

    Or just to explain that segment more with more of the code, the initial string is in var1.

    Code:
    
    
    
    					sscanf(var1,"Seat %d: %s %[^\n]\n",&seatnum,player,var2);
    					{
    						int a1;
    						for(a1=0; var2[a1]!=EOF; a1++)
    						{
    							if(var2[a1]=='w' {
    								if (strcmp(&var2[a1],"won" ==0) {
    									printf("\n FOund it!! %s won, player");	
    									break;
    								}
    								
    							}
    						
    						}
    					
    					}
    So basically I just search for a 'w' and then do a strcmp for "won" that should do the trick
    Strstr is probably better, actually now I have read the description of it it is ideal!

    I will use strstr if my code fails!! (probably will!!).
    I don't know the libaries very well, I probably should but usually find it quicker to write the
    code myself than search for the correct function!! However in the long run it would be better
    to know the libaries!!
    Thanks for your help!!!

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't believe the above code would work. Think of what would happen with the string "akjldfhwondjkhjfad".
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("\n FOund it!! %s won, player");
    The " is misplaced.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Found some bugs!!

    Code:
    
                                                                   sscanf(var1,"Seat %d: %s %[^\n]\n",&seatnum,player,var2);
    					{
    						int a1;
    						for(a1=0; var2[a1]!=EOF; a1++)
    						{
    							if(var2[a1]=='w') {
    								if (  strcmp(&var2[a1],"won") ==0) {
    									printf("\n FOund it!! %s won, player");	
    									break;
    								}
    								
    							}
    						
    						}

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Happy_Reaper
    I don't believe the above code would work. Think of what would happen with the string "akjldfhwondjkhjfad".
    Fair point. If the strings were of a generalized nature if would fail, however they are very specific and limited in their context and "won" should only occur as " won ", well I hope so anyway!!

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks
    Code:
    printf("\n FOund it!! %s won, player");
    The " is misplaced.
    Well spotted Sherlock!! (as in Sherlock Holmes).
    Actually it does compile, but "other bugs" stop it getting that far!!

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by esbo
    Fair point. If the strings were of a generalized nature if would fail, however they are very specific and limited in their context and "won" should only occur as " won ", well I hope so anyway!!
    Even at that. Remember that "won" and "won " are not the same string.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF cannot be stored in a char, so it will likely trample off the end of your string and across whatever memory follows it until it happens to find something that evaluates to its interpretation of EOF. Or until it crashes.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Well spotted Sherlock!! (as in Sherlock Holmes)
    Thanks for clearing that one up.

    jk
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by psychopath
    Thanks for clearing that one up.

    jk
    Well I like to be verbose, osme people might not "get it", I find it annoying for example when some people use acronyms, which I do not know, without expanding them.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by quzah
    EOF cannot be stored in a char, so it will likely trample off the end of your string and across whatever memory follows it until it happens to find something that evaluates to its interpretation of EOF. Or until it crashes.


    Quzah.

    Well in the 'early days' if probably couuld have, things have moved on since then I guess;
    Anyway I have 'dumped' my code and I am using strstr as dwks recommended
    (the code I wrote didn't!!)

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, in the early days it didn't fit in a char either.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Thanks for all your help guys, I have got the code working in a fashion, probably a bug or two still left in there though but the code I will post next shows I can identify the winner of a poker tournement.
    The first 'field' before the "-1" is the tournement name, and the winner of a particular hand is shown after that. (In some hands there is no winner because nobody bets)
    The key point is that the last player to be shown winning win has won the tournement, so the last player show as winning before the tournement name/number changes won the tournement.

    So you can see:-
    robster4 won the first one.
    Vol1976 won the second
    Theego001 won the third
    and My_Poker_ID won the last two.

    (I also came second in one of the tournents, and third in the other two) You get a payout for coming second, 1.5 times your entry fee, (you get 4.5 times it for winning)), so my total winnings were 10.5 'points' and it cost me 5 points in total to enter, a return of £10.50 from £5, which is nice (well actuall from £7.50 because you pay a £0.5 admin fee!!)
    Of course I don't always do that well, but now I have this program I can see how I do over thousands of games (it will take a while to run!!). I might give up playing the tournements
    because the admin fee is 10%, as opposed to 5% in other games. But tournements are more fun!!

    It is actually a modification of a program I wrote to analyse my winnings in none
    tournement game (a much more compllicate program which took a fair time to write!! but works well, showing lots of statistics on my and my opponents play).
    I will futher modify this tournement program to show how well I and other players do, easier in comparision because you only have to find who wins and comes second, in none tournement games you have to process every individual bet and win in every game!!

    I might even post the source code if anyone is interested!! (600 lines of badly written code but it does work well!!)

    The program does not really help you a great deal in actually playing poker, but it can be helpfull to see who the good and poor players are (sometimes). However you need hell of a lot of hands to distinguish between bad play and bad luck!!
    So its better just to concentrate on playing well.

    The programs out follows (I changed my real poker ID )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM