0

The goal of the method is to displace each character in str by n.

void *rotate_string_in_place(char* str, unsigned int disp){
    for(int i = 0; i < strlen(str); i++){
        if(str[i] == '\0') break;
        if(str[i] == ' ') continue;
        if(str[i] < 'A') continue;
        if(str[i] > 'Z' && str[i] < 'a') continue;
        if(str[i] > 'z') continue;
        unsigned int diff = 0;
        if(str[i] >= 'A'&& str[i] <= 'Z'){
            if((str[i]+disp) > 'Z'){
                diff = 'Z' - str[i];
                str[i] = 'A'+(disp-diff-1);
                continue;
            }
            str[i] = str[i] + disp;
            continue;
        }
        if(str[i] >= 'a' && str[i] <= 'z'){
            if((str[i]+disp) > 'z'){
                diff = 'z'-str[i];
                str[i] = 'a'+(disp-diff-1);
                continue;
            }
            str[i] = str[i] + disp;
            continue;
        }
    }
    printf("%s\n", str);
}

However on the line where I actually try to set the char to a new value,

str[i] = str[i] + disp;

I get a segfault.

Barmar
  • 741,623
  • 53
  • 500
  • 612
macman926
  • 19
  • 7
  • You don't need `if(str[i] == '\0') break;` since `strlen()` will stop before the null byte. – Barmar Feb 22 '20 at 17:52
  • 1
    You need to show us the code from which the `rotate_string_in_place` is called! Maybe you're passing it a string literal? This would cause an error when you try to modify any of its elements. – Adrian Mole Feb 22 '20 at 17:52

0 Answers0