I need to get the 3 letters right of a string, the extension. Is there a function in string.h that can do that??
I used Right and Left in Visual Basic.
I need to get the 3 letters right of a string, the extension. Is there a function in string.h that can do that??
I used Right and Left in Visual Basic.
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
I don't believe there's a standard one, but it'd be easy enough to build one yourself.......
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
No (In ANSI C atleast). But you can write your own with very little ease using the standard string functions.
Has anyone made something similar? I dont want the actual code, but which functions did you use to make it?
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
>Has anyone made something similar? I dont want the actual code, but which functions did you use to make it?
It depends on the way you implement it. strlen() is one which is real helpful.
One way of implementing is, extract all the characters individually from the string in the range: [strlen(string) - 4] to [strlen(string)]
Just remember to account for strings that don't have enough characters (ie empty ones)........
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Well if you start with
char filename[] = "wibble.txt";
Then you can locate the dot with
char *p = strchr( filename, '.' );
Or locate the .txt with
char *p = strstr( filename, ".txt" );
In both cases, p either points to the dot, or p is NULL indicating that nothing was found.
Oh thank you Salem, it worked strchr was the solution i needed!
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
Unless you're using DOS (or potentially some other OS), you don't know for sure that there is only one "." in the file name.
Example: In *nix, it's common to have:
".tar.gz" on the end of the file.
One way is simply to loop through the string from the end. (Basicly strchr does the same thing, just from the beginning.)
Now, you could use strchr. Just use it with more than one character. Or, even better, use it in a loop:Code:for( x = strlen( s ) -1; x > -1; x-- ) { if( s[x] == charToFind ) break; }
That should do it.Code:c = strchr( string, tofind ); for( temp = c; temp != NULL; temp = strchr( c, tofind ) ) if( temp != NULL ) c = temp;
Quzah.
Hope is the first step on the road to disappointment.
Correct me if I'm wrong, but wouldn't it be simpler to use strrchr().
For example:
Code:#include <stdio.h> #include <string.h> int main(void) { char x[] = "filename.txt"; char *p; if ((p = strrchr(x, '.')) == NULL) printf ("No extension\n"); else printf ("Extension is %s\n", p+1); return 0; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Sure. That'd work. However, are either of those two functions in the standard? My reference may be outdated, but it doesn't liste them as ANSI functions... (While it may be no big deal to the original poster, I'm generally pick regarding this.)
Quzah.
Hope is the first step on the road to disappointment.
Yes they are ANSI functions
I need updated man pages then...Originally posted by Salem
Yes they are ANSI functions
Quzah.
Hope is the first step on the road to disappointment.
What 2 functions? I only used strrchr() and printf()? !! (I'm just being sarcastic, I guess you meant strchr() )Originally posted by quzah
Sure. That'd work. However, are either of those two functions in the standard? My reference may be outdated, but it doesn't liste them as ANSI functions...
Also, if your documentation doesn't liste them, it's probable that you have Ye Olde English version... I definately recommend updating.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I just use the man pages while at work for quick reference. Some one needs to update them. (They don't list strchr and strrchr as conforming to ANSI.) Still, over all they're a pretty good reference.
(I've never seen a compiler that didn't have strchr, and had never looked for or used strrchr--actually, I very seldom need either function [never in the case of strrchr]--but I find it's better to not assume something is ANSI and find out later that it isn't.)
Quzah.
Hope is the first step on the road to disappointment.