compare issue

Discussion in 'Mac Programming' started by dukebound85, Oct 17, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    Hi all, how would you check to see if there was an extension on a string using strcmp?

    thanks for any pointers
     
  2. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #2
    Personally I wouldn't use strcmp to check for an extension. It is more efficient to just search backwards through the string until you find the extension character such as '.'.

    However if you want to use strcmp then this is one way to do it:

    Code:
    #include <stdio.h>
    
    main() {
        char extension[30] = ".foo";
        char string[30] = "something.foo";
    
        int i;
    
        for(i=strlen(string); i >= strlen(string) - strlen(extension); i--) {
            if(!strcmp(extension, string+i)) {
                printf("match\n");
            }
        }
    }
    
     
  3. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #3
    If efficiency is the worry, strlen() inside the for() statement would be best avoided. Stuff the results of the expressions into variables first and then loop on those.
     
  4. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #4
    Yes, but if all you are checking is whether the string has an extension then I wouldn't use strcmp at all:

    Code:
    int i;
    
    for(i = strlen(string) - 1; i >= 0; i--) {
        if(string[i] == '.') {
            printf("string has extension starting at character %d\n", i);
            break;
        }
    }
    
     
  5. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #5
    thanks however i cant seem to get it to work.

    if you read in a string to a function, is there a way to cut out the last say 3 digits(as in the extension) save that as a string then cat the extension on and compare it to the original string?

    say str1[30]="cars.jpg";

    strcpy(str2,str1);

    str2[strlen(str2)-4] to cut out last 4

    then

    strcat(str2,".jpeg")

    strcmp(str2,str1)


    am i on the right track at all?

    once again thanks to you all
     
  6. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #6
    Here is a little function to do the job with a test program attached.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* check if s1 ends with s2; return 1 if match else 0 */
    int
    endswith(char *s1, char *s2)
    {
        char *s1end, *s2end;
        
        s1end = s1 + strlen(s1) + 1;
        s2end = s2 + strlen(s2) + 1;
        while (--s2end >= s2 && --s1end >= s1) {
            if (*s1end != *s2end)
                return 0;
        }
        if (s2end < s2)
            return 1;
        return 0;
    }
    
    int
    main()
    {
        char* filespec[] = { "New Powerbooks next Tuesdayxt",
                            "I like snails.txt, I really do",
                            "realfile.txt",
                            "autoexec.tx",
                            "txt",
                            "this is text.txt",
                            ".txt",
                            "x",
                            ""
                           };
        int i;
        
        for (i=0; *filespec[i]; i++) {
            printf("%s: %s\n", filespec[i],
                   endswith(filespec[i], ".txt") ? "yes" : "no");
        }
        return 0;
    }
    
     
  7. mindwalkernine macrumors newbie

    Joined:
    Jul 23, 2006
    #7
    In Objective-c, this is just a start;

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[])
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        NSString *xor = @".xor";
    	
    	NSString *fileName = @"myEncryptedFile.xor";
    	
    	int fileNameLength = [fileName length];
    	
    	NSString *extension = [fileName substringFromIndex:fileNameLength-4];
    	
    	if( [extension isEqualToString:xor] )
    		NSLog(@"Extension is a .xor extension.");
    	
        [pool release];
        return 0;
    }
     
  8. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #8
    Here is the same function using strcmp() incase you needed to see it done that way.

    Code:
    /* check if s1 ends with s2; return 1 if match else 0 */
    int
    endswith(char *s1, char *s2)
    {
         size_t s1len, s2len;
        
         s1len = strlen(s1);
         s2len = strlen(s2);
    
         /* a longer string will never fit inside a shorter one. */
         if (s2len > s1len)
              return 0;
    
         /* scoot s1 over so the length matches s2 */
         s1 += (s1len - s2len);
    
         return !strcmp(s1, s2);
    }
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Except in Objective-C you have the "pathExtension" method for NSString ;)
     

Share This Page