内容简介:输出结果为:输出结果为:
- 使用字符数组存储字符串,\0 表示结束符,字符串可以被修改
void main(){
char str[] = {'c','h','i','n','a','\0'};
//char str[6] = {'c','h','i','n','a'};
//char str[10] = "china";
printf("%s\n",str);
str[0] = 's';
printf("%s\n",str);
printf("%#x\n",str);
getchar();
}
输出结果为:
china shina 0xd9fd64
- 使用字符指针存储字符串,字符串不能被修改
void main(){
//内存连续排列
char *str = "how are you?";
//字符串不能被修改
//str[0] = "w";
//
// str += 1;
//*str = 'y';
printf("%s\n",str);
printf("%#x\n",str);
getchar();
}
输出结果为:
how are you? 0x967be4
2. 字符串相关函数
- strcat 字符串拼接函数
- strcpy 字符串复制函数
void main(){
char dest[50];
char *a = "china";
char *b = " is powerful!"
//将数组a复制到数组dest中
strcpy(dest,a);
//将数组b拼接到数组dest上
strcat(dest,b);
printf("%s\n",dest);
getchar();
}
输出结果为:
china is powerful!
- strchr 在一个字符串中查找给定字符的第1个匹配的之处
void main(void){
char *haystack = "I want go to USA!";
char *needle = "to";
//U元素的指针
char* p = strstr(haystack, needle);
if (p){
printf("索引位置:%d\n", p - haystack);
}
else{
printf("没有找到");
}
system("pause");
}
- strcmp 比较字符串
- strcmpi 比较字符串,忽略大小写
void main(void){
char *str1 = "abc";
char *str2 = "ABC";
//int r = strcmpi(str1, str2);
int r = _strcmpi(str1, str2);
printf("%d\n",r);
//str1 > str2
if (r > 0){
printf("str1 大于str2\n");
}
else if (r == 0){
printf("str1 等于str2\n");
}
//str1 < str2
else if (r < 0){
printf("str1 小于str2\n");
}
system("pause");
}
- strset 把字符串s中的所有字符都设置成字符c
void main(void){
char str[] = "internet change the world!";
_strset(str,'w');
printf("%s\n",str);
system("pause");
}
- strrev 把字符串s的所有字符的顺序颠倒过来
void main(void){
char str[] = "internet change the world!";
_strrev(str);
printf("%s\n", str);
system("pause");
}
- atoi 字符串转为int类型
- atol():将字符串转换为长整型值
void main(void){
char* str = "a78";
//int r = atoi(str);
printf("%d\n", r);
system("pause");
}
- strtod:字符串转为double类型
void main(void){
char* str = "77b8b";
char** p = NULL;
//char* p = str + 2;
//参数说明:str为要转换的字符串,endstr 为第一个不能转换的字符的指针
double r = strtod(str,p);
printf("%lf\n", r);
printf("%#x\n", p);
system("pause");
}
- strupr转换为大写
void main(void){
char str[] = "CHINA motherland!";
_strupr(str);
printf("%s\n",str);
system("pause");
}
- 转换为小写
void mystrlwr(char str[],int len){
int i = 0;
for (; i < len; i++){
//A-Z 字母 a-Z
if (str[i] >= 'A' && str[i] <= 'Z'){
str[i] = str[i]-'A' + 'a';
}
}
}
以上所述就是小编给大家介绍的《(三)C语言之字符串与字符串函数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
史蒂夫·乔布斯传
[美] 沃尔特·艾萨克森 / 管延圻、魏群、余倩、赵萌萌、汤崧 / 中信出版社 / 2011-10-24 / 68.00元
这本乔布斯唯一授权的官方传记,在2011年上半年由美国出版商西蒙舒斯特对外发布出版消息以来,备受全球媒体和业界瞩目,这本书的全球出版日期最终确定为2011年11月21日,简体中文版也将同步上市。 两年多的时间,与乔布斯40多次的面对面倾谈,以及与乔布斯一百多个家庭成员、 朋友、竞争对手、同事的不受限的采访,造就了这本独家传记。 尽管乔布斯给予本书的采访和创作全面的配合,但他对内容从不干......一起来看看 《史蒂夫·乔布斯传》 这本书的介绍吧!
UNIX 时间戳转换
UNIX 时间戳转换
HEX CMYK 转换工具
HEX CMYK 互转工具