I am trying the get the file extension of a file in lowercase in C.
I have seen the following links:
- Getting file extension in C
- https://www.includehelp.com/c-programs/c-program-to-convert-string-into-lower-and-upper-case.aspx
and have combined them to the following.
#include <stdio.h>
#include <string.h>
char *get_filename_ext(char *filename) ;
void stringLwr(char *s) ;
int main(void) {
char *ext ;
ext = get_filename_ext("test.PDF") ;
printf("Ext: %s\n", ext) ;
stringLwr(ext) ;
printf("Ext: %s\n", ext) ;
return 0;
}
char *get_filename_ext(char *filename) {
char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
void stringLwr(char *s){
int i=0;
while(s[i]!='\0'){
if(s[i]>='A' && s[i]<='Z'){
s[i]=s[i]+32;
}
++i;
}
}
What I except is Ext: PDF
followed by Ext: pdf
. However, I am getting
Segmentation fault (core dumped)
after the first line. I know it has to do with the pointer, but I am unable to figure it out. Any help would be appreciated.