LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C....Search a string for a string (https://www.linuxquestions.org/questions/programming-9/c-search-a-string-for-a-string-192765/)

Scrag 06-12-2004 02:47 PM

C....Search a string for a string
 
Does anybody know of a function or have a code example that searches a string, for contents of another string? For example, I would like C program to look at file names, and be able to search each "filename/string" for ".exe".

Thanks.

:study:

The_Nerd 06-12-2004 02:57 PM

Code:

if (strstr(myfilename, ".exe")!=NULL)
{
    //Is an exe
}

strstr returns a pointer to the place in string1 where string2 was found.

Enjoy!

Scrag 06-12-2004 03:21 PM

Thanks!!

itsme86 06-13-2004 09:52 AM

If you want to make sure that ".exe" are the last 4 characters in the filename, then this approach might be better:

Code:

if(strlen(filename) > 4)
  if(strcmp(filename+strlen(filename)-4, ".exe") == NULL)
  {
    // It's a .exe
  }


The_Nerd 06-14-2004 04:15 PM

Quote:

Originally posted by itsme86
If you want to make sure that ".exe" are the last 4 characters in the filename, then this approach might be better:

Code:

if(strlen(filename) > 4)
  if(strcmp(filename+strlen(filename)-4, ".exe") == NULL)
  {
    // It's a .exe
  }


Actually...

Since he is doing this for Windows (at least I think he is, why else .exe?), then this would probably be even better:

Code:

if(strlen(filename) > 4)
  if(stricmp(filename+strlen(filename)-4, ".exe") == NULL)
  {
    // It's a .exe
  }



All times are GMT -5. The time now is 12:42 PM.