I am trying to write a function which will read two strings stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI"
and popArr[MAX]="ABCDEFGHIJ"
and generate an output like this:
A
B-F-D-A
C-F-D-A
D-A
E-G-C-F-D-A
F-D-A
G-C-F-D-A
H-C-F-D-A
I-J-H-C-F-D-A
J-H-C-F-D-A
However I'm getting a Segmentation fault (core dumped)
Error. Why? This is my code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
size_t strlstchar(const char *str, const char ch)
{
char *chptr = strrchr(str, ch);
return chptr - str;
}
int main(){
// Input strings
char stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI";
char popArr[MAX]="ABCDEFGHIJ";
int index=2, lenpop, lentemp;
char usedString[MAX]="";
char tempChar;
lenpop = strlen(popArr);
printf("%c\n", stringArray[0]);
for(int i=1;i<lenpop;i++){
strcpy(usedString, stringArray);
printf("%c", popArr[i]);
tempChar = popArr[i];
while(tempChar!=stringArray[0]){
while(index%2==0){
index = strlstchar(usedString, tempChar);
lentemp = strlen(usedString);
usedString[lentemp-index-1]=0;
}
printf("-%c", usedString[index-1]);
tempChar=usedString[index-1];
index=2;
}
printf("\n");
}
return 0;
}
Thanks in advance!