Get all strings until the last occurrence of a certain character.

Hi All,

Is there a fast way in C to get all string until last occurrence of a certain character. for example I have string:

'home/user/dir/file.txt'

I want the result (until last .):

'home/user/dir/file'

Here number of slash is dynamic also file name might be in any legal format ( including special chars and white space).

Thanks in advance,
Umit
Last edited on
I found my answer. Thanks!!!!

In case someone may need

char * pch;
char full_path[100];
char pathWoExt[100]; // path without extention

pch = strrchr(full_path,'.'); // finds last occurrence of .

/* partial copy until specified char which is . in this case */
strncpy ( str3, full_path, pch-full_path);
str3[pch-full_path] = '\0'; /* null character manually added */
puts(str3);
closed account (Dy7SLyTq)
why not use c++ with <string>?
Topic archived. No new replies allowed.