I have a problem with this code. I want to make stringICmp with two char pointers, but I get an error after the first if (Program received SIGSEGV, Segmentation fault.). I use Windows 10 with GDB Compiler. I googled SIGSEGV, but I've no clue how to solve that correctly, thanks for help.
#include <stdio.h>
#include <stdlib.h>
#define MAX_STR 20
int stringICmp(char *s1, char *s2);
int main() {
char *str1 = "Hallo";
char cmpstring[MAX_STR] = "HaLlo";
int sicmp = stringICmp(str1, cmpstring);
printf("\n\nString Compare non case-sensitive:\n");
printStringCmp(sicmp);
}
void printStringCmp(int scmp) {
printf("\nThe String Compare Result is: %d\n", scmp);
if(scmp > 0) {
printf("String 1 is bigger than String 2.");
} else if(scmp < 0) {
printf("String 1 is smaller than String 2.");
} else if(scmp == 0) {
printf("String 1 is equal to String 2.");
}
}
int stringCmp(char *s1, char *s2) {
while(*s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
s1++;
s2++;
}
if(*s1 > *s2) {
return 1;
} else if(*s1 < *s2) {
return -1;
}
return 0;
}
int stringICmp(char *s1, char *s2) {
char *s1cpy = s1, *s2cpy = s2;
while(*s1cpy != '\0' && *s2cpy != '\0'){
if(*s1cpy >= 65 && *s1cpy <= 90) {
(*s1cpy) += 32;
}
if(*s2cpy >= 65 && *s2cpy <= 90) {
(*s2cpy) += 32;
}
s1cpy++;
s2cpy++;
}
s1 = s1cpy;
s2 = s2cpy;
int scmp = stringCmp(s1, s2);
return scmp;
}