Thread: Help with search <string.h>

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Help with search <string.h>

    What I am trying to do is be able to search for a variable starting with a chosen character using the <string.h> header.

    The code so for......

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char **argv, char** envp) 
    {
      char** en;
      for (en = envp; *en != 0; en++)
      {
        char* ce = *en;
        printf("%s\n", ce);       
      }
      system("PAUSE");    
      return 0;
    }
    Any one help?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's super easy.... look up the strchr() function in your compiler's library docs.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Using strchr() on your char, will find any instance of the char, but it won't tell you whether the char is the first char of a word.

    You may need to separate every word, and then check if word[0]== the char you're looking for.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Thanks for that.

    but

    That code I have at the moment just prints out the enviroment variables, how can I manipulate the data so I can use strchr to print the result of the search?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What, exactly, are you trying to accomplish with this search?

    Are you trying to separate the vairable name from it's data?
    Are you trying to find specific variable names?

    What?

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Sorry for the ambiguity. I am trying to get the enviroment variables as a list and then be able to search for specific variable and output the result.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So maybe, strstr() is what you want. Looks for a string, inside a larger string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 06:51 PM
  2. String Search
    By slowprogrammer in forum C Programming
    Replies: 5
    Last Post: 05-25-2010, 12:39 AM
  3. string search
    By victor_miami in forum C Programming
    Replies: 6
    Last Post: 10-24-2006, 08:56 PM
  4. String Search
    By johnchain in forum C Programming
    Replies: 9
    Last Post: 09-29-2005, 05:37 PM
  5. String Search .....
    By cooleeze in forum C++ Programming
    Replies: 5
    Last Post: 10-19-2001, 02:34 PM