Increment the first byte of a string by one

后端 未结 3 1748
庸人自扰
庸人自扰 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:58

    char *str = "hello"; is a unmodifiable string literal.

    The representation for h and i in ASCII for various bases are:

      dec  char bin         hex  oct
      104.    h 0110 1000  0x68 0150
      105.    i 0110 1001  0x69 0151
    

    As you can see, bit 1 is flipped from h to i, so if you want to change it by bit operations one way would be:

    #include <stdio.h>
    
    int main(void) 
    {
        char str[] = "hello";
    
        printf("%s\n", str);
        str[0] |= 0x01;
        printf("%s\n", str);
    
        return 0;
    }
    

    To use the increment use:

    ++str[0];
    

    or:

     char *p = str;
     ++*p;
    

    Same, both increment and bit handling, goes for upper case.


    If you work with the ASCII set there are other nice properties. As one example:

    dec  char bin         hex  oct
     65. A    0100 0001  0x41   101o
     66. B    0100 0010  0x42   102o
     67. C    0100 0011  0x43   103o
     68. D    0100 0100  0x44   104o
                |
                +--- flip bit 5 to switch between upper and lower case.
                     This goes for all alpha characters in the ASCII set.
     97. a    0110 0001  0x61   141o
     98. b    0110 0010  0x62   142o
     99. c    0110 0011  0x63   143o
    100. d    0110 0100  0x64   144o
    

    Thus:

    char str[] = "hello";
    
    str[0] ^= 0x20;
    printf("%s\n", str);    /* Print Hello */
    
    str[0] ^= 0x20;
    printf("%s\n", str);    /* Print hello */
    

    Another one more frequently used, and which also is the same for e.g. EBCDIC, are the properties of numbers. They have ordered distance from 0, and in continuous range, so:

    char num[] = "3254";
    int n1 = num[0] - '0'; /* Converts char '3' to number 3 */
    int n2 = num[1] - '0'; /* Converts char '2' to number 2 */
    etc.
    

    You can expand this for ASCII when converting string representation of hex values to numbers as the alpha characters are in order as well:

    unsigned hex_val(char c)
    {
        if (c >= '0' && c <= '9')
            return c - '0';
        if (c >= 'a' && c <= 'f')
            return c - 'a' + 10;
        if (c >= 'A' && c <= 'F')
            return c - 'A' + 10;
        return ~0;
    }
    

    OK. I'll better stop there perhaps …

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-26 07:10

    Do something like:

    #include <stdio.h>
    #include <ctype.h>
    
    int main() {
    char str[] = "hello";
    printf("%s\n", str);
    
    //str[0]=str[0]+1;
    
    // to get uppercase letter include ctype.h header
    
    str[0] = toupper(str[0] + 1);
    
    printf("%s\n", str);
    return 0;
    }
    
    0 讨论(0)
提交回复
热议问题