0

I've got a main program:

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

    /* Have tried
    str[0] >>= 8; */
    printf("%s\n", str);
    return 0;
}

Basically shift the first byte of the string up a byte to go from hello to iello or Hello to Iello

Cœur
  • 37,241
  • 25
  • 195
  • 267
tcatchy
  • 849
  • 1
  • 7
  • 17
  • Read [Difference between `char *str` and `char str[]` and how both stores in memory?](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Nov 05 '13 at 06:45

3 Answers3

4

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);
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

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 …

Runium
  • 1,065
  • 10
  • 21
2

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;
}
user1502952
  • 1,390
  • 4
  • 13
  • 27