Thread: Searchinh With In A String

  1. #1
    Registered User
    Join Date
    Jul 2002

    Searchinh With In A String

    how can you search threw a string more than once? im working on a program where you can type in options on the command line o type them when the program starts. how could i serach for the command flag and the arg?

    why doesnt this work?
    while(input[x] != EOF) {
    	if(input[x] == '-' && input[x+1] == 't') {
    		x+= 3;
    		while(input[x] != ' ') {
    			temp[y] = input[x];
    or better yet is there a way to take the input string and turn it into something argv in main?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >how could i serach for the command flag and the arg?
    What you have would work if you changed the outer loop test. When testing for the end of a string you check for the nul terminator, '\0'. Or you could use strstr to search for the letter combinations:
    if ( ( arg = strstr ( input, "-t" ) ) != NULL ) {
      arg += 3;
      while ( !isspace ( *arg ) )
        temp[y++] = *arg++;
    As for taking both command line and program input, you could check argc at the beginning of the program. If argc is equal to 1 then there are no command line arguments and you should take program input, otherwise process argv.

    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    im checking for the argc count in my prgram, thanks anyways...

    and with your idea of using strstr, the pointer address the individual character? this could simplify things
    Last edited by mart_man00; 08-22-2002 at 02:25 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    strstr returns a pointer to the first character of the substring you're looking for, or NULL if it is not found. Example:

    ptr = strstr( "hello there", "the" );

    The pointer will point to the t.

    If you are just looking for a single character each time, you could always use strchr which works exactly the same way, except the second argument is a single character:

    ptr = strchr( "hello there", 'l' );

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM