将字符串的第一个字节加1 [英] Increment the first byte of a string by one
本文介绍了将字符串的第一个字节加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;
}
基本上将字符串的第一个字节上移一个字节,从hello
到iello
或Hello
到Iello
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屋!
查看全文