Increment the first byte of a string by one

后端 未结 3 1781
庸人自扰
庸人自扰 2021-01-26 06:16

I\'ve got a main program:

int main() {
    char *str = \"hello\";
    printf(\"%s\\n\", str);
    /* Shift first byte 1 to get \"iello\" */

    /*          


        
3条回答
  •  难免孤独
    2021-01-26 06:59

    You can't modify a string literal, change it to:

    char str[] = "hello";   //not string literal
    printf("%s\n", str);
    str[0]++;              //add 1 to the first element
    printf("%s\n", str);
    

提交回复
热议问题