将字符串的第一个字节加1 [英] Increment the first byte of a string by one

查看:155
本文介绍了将字符串的第一个字节加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个main程序:

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;
}

基本上将字符串的第一个字节上移一个字节,从helloielloHelloIello

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

推荐答案

您无法修改字符串文字,请将其更改为:

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);

这篇关于将字符串的第一个字节加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆