Thread: Find String inside Array?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question Find String inside Array?

    Hello, I want to search for a string inside one array, and get the position (index) of the first (or last) char when this was found... I tryed this searching for "play":

    Code:
    		   while (i<(TamNew-5)){ //find if this is a spawn class
    				if((NewStr[i]=="p")&&(NewStr[i+1]=="l")&&(NewStr[i+2]=="a")&&(NewStr[i+3]=="y"))
    				{
    					p=i;
    					break;
    				}
    				i++;
    		   }
    but didn't work! I tryed before this:


    Code:
    char *p;
    p = strstr(NewStr, "play");
    but P return the mem address, not the array index that I need!

    any clues?

    thanks!!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    p-NewStr gives you an index
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Code:
    char *loc;
    loc=p-NewStr;
    printf("&#37;d",loc);
    will give you the index of the first char in the string
    ------
    seems like me and vart posted the same replay in the same time

  4. #4
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    didn't work, make the game abort... any other way? maybe without use pointers?

    thanks!

  5. #5
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    NewStr is a array... could be that?

    char NewStr[150];

  6. #6
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    i guess it should be
    Code:
    char *loc;
    loc=New_str-p;
    printf("&#37;d",loc);
    it works with me

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by hajas View Post
    NewStr is a array... could be that?

    char NewStr[150];
    what do you mean !!!!!!!!!!!

  8. #8
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    seams that I can't create pointers... is just create one more than the game abort.

    is there a way to do that search? why not working the i+1 index that I did? I remember to do this a lot in the past...

  9. #9
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    worked now!

    Code:
    char	*ptr = malloc(151);
    
    			   strcpy(ptr,NewStr);
    			   origem = strstr(ptr, "origin"); //find the origin position
    			   loc = origem-ptr;
    			   free(ptr);
                   ori = loc + 8;
    ori = 12! perfect! thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM