There are two problems in your code 1) You are using the char * are are trying to modify that protected memory inside your code
2) You are trying to return a local address from your function which is also not good.
3) What if the filename contains a DOT '.' in its filename.
You can use char array as a solution but still the second problem remains, for that you should allocate some memory and then copy the returning string into that memory
char* swapExtension(char * fileName, char * fileExtension)
{
int newext = strlen(fileExtension);
bool flag =false;
int size = strlen(fileName);
int ext = 0;
for(int i=size-1;i>=0;i--)
{
if(fileName[i] == '.')
{
flag = true;
ext = (size -1) - i;
break;
}
}
if(!flag){
printf("no extension found");
return NULL;
}
int retsize = size - ext + newext + 1;
char * retBuff = new char[retsize];
retBuff[0]='\0';
memcpy(retBuff,fileName,retsize);
strncpy(&retBuff[size-ext], fileExtension, newext);
return retBuff;
}