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:


I want the result (until last .):


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

Thanks in advance,
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 */
closed account (Dy7SLyTq)
why not use c++ with <string>?
Topic archived. No new replies allowed.