Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
dukebound85 said:
Hi all, how would you check to see if there was an extension on a string using strcmp?

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");
        }
    }
}
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
iMeowbot said:
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.

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;
    }
}
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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;
}
 

mindwalkernine

macrumors newbie
Jul 23, 2006
9
0
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;
}
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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);
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
mindwalkernine said:
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;
}

Except in Objective-C you have the "pathExtension" method for NSString ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.