0

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;
}
user316860
  • 23
  • 5
  • Please show how you call `stringICmp`, the origin of the problem is most likely _there_. Also a [mcve] would be helpful here. And what is `stringCmp`? – Jabberwocky Nov 07 '18 at 16:16
  • 1
    Also avoid magic numbers such as `65`, write `'A'` instead of `65` etc. – Jabberwocky Nov 07 '18 at 16:17
  • 1
    You also should consider functions like [tolower](https://www.programiz.com/c-programming/library-function/ctype.h/tolower). – Osiris Nov 07 '18 at 16:18
  • Another hint: `char *s1cpy = s1` isn't making a copy of the string, it just copies the _pointer_ to the string which is pointless here. – Jabberwocky Nov 07 '18 at 16:20
  • When I used tolower(), I've also got the same Error. – user316860 Nov 07 '18 at 16:25
  • And yeah, I know that `char *s1cpy = s1;` just makes a copy of the pointer. I need the starting address at the end. Or not? I'm a bit confused now. – user316860 Nov 07 '18 at 16:30
  • The actual reason why you've got the SIGSEGV is explained in [this SO article](https://stackoverflow.com/q/164194/898348). Please [edit] your question and show also `stringCmp` – Jabberwocky Nov 07 '18 at 16:44
  • Okay, thank you guys for your help. – user316860 Nov 07 '18 at 16:52
  • 1
    Hint: throw away the `stringICmp` function and rewrite it from scratch based on `stringCmp`. – Jabberwocky Nov 07 '18 at 16:57
  • 1
    +1 to @Jabberwocky 's hint to base `stringICmp` on your existing `stringCmp`; one small change is all you need to make the comparison case-insensitive; I see your motivation to re-use `stringCmp` from inside `stringICmp`, but the string duplication and manipulation is costing you more in terms of code complexity than having a nearly-identical `stringICmp` – landru27 Nov 07 '18 at 17:16

1 Answers1

0

the reason your code is failing is because you are trying to modify a character constant

int main() {
    char *str1 = "Hallo";

..
int sicmp = stringICmp(str1, cmpstring);
..

int stringICmp(char *s1, char *s2) {
    char *s1cpy = s1, *s2cpy = s2;
    while(*s1cpy != '\0' && *s2cpy != '\0'){
        if(*s1cpy >= 65 && *s1cpy <= 90) {
            (*s1cpy) += 32; <<<< right here you try to overwrite your inpout

THis is not allowed. You should operate on a copy of the string (also you are destroying your input, not a polite thing to do)

Use strdup to copy the input string.

int stringICmp(char *s1, char *s2) {
    char *s1cpy = strdup(s1), *s2cpy = strdup(s2);
    while(*s1cpy != '\0' && *s2cpy != '\0'){
        if(*s1cpy >= 65 && *s1cpy <= 90) {
            (*s1cpy) += 32;
....
free(s1cpy);
free(s2cpy);
pm100
  • 48,078
  • 23
  • 82
  • 145