C/C++字符串

来源:互联网 发布:ubuntu命令行解压rar 编辑:程序博客网 时间:2024/06/03 20:04
# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char str1[3] = {'a','b','c'};//错误,因为没有"\0"终止符,如果后面不为0则会出现异常    printf("%x\n", str1);    printf("%s\n",str1);    char str2[4] = {'a','b','c','\0'};//最后一个赋值为\0,打印终止    printf("%x\n", str2);    printf("%s\n",str2);    char str3[5] = {'a','b','c'};//数组位赋值的默认为0,而ascii编号为0代表的就是\0    printf("%x\n", str3);    printf("%s\n",str3);    printf("%d %d",sizeof("abc"),sizeof(str3));    //"abc"字符串长度为4,因为最后一位是\0,而str是一个数组所以长度为5,但打印字符串时    //遇到\0会停止,所以也会打印出"abc"}2cc929b0abc2cc929c0abc2cc929d0abc4 5

字符串常量

# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char *str = "hello world";    printf("%d\n",sizeof(str));    printf("%d\n",sizeof("hello world"));    printf("%x\n%c\n",str,*str);    int i=0;    while(*str){//*str为\0的时候停止(*str!="\0",\0既是ascii中的0,这里注意space空格ascii是32        putchar(*str);//输出字符串        str++;//指针向前移动1位        i++;//移动一位计数1    }    printf("\n%d",i);//打印字符串长度,利用"\0"结尾这一特性}

字符串变量(利用数组)

# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char str[12]= "hello world";    int i=0;    while (str[i]){        putchar(str[i]);        i++;    }    printf("\n");    char *p = str;    while (*p){//纯指针移动打印        putchar(*p);        p++;    }    printf("\n");    char *q = str;    while (q[0]){//q[0]等价于*(q+0),指针数组的方式        putchar(q[0]);        q++;    }}

字符串初始化

# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char str1[100] = {'h','e','l','l','o',' ','w','o','r','l','d'};//每个数组元素赋值    char str2[100] = {'h','e','l','l','o',' ','w','o','r','l','d','\0'};//每个数组元素赋值    char str3[100] = {"hello world"};//直接赋值    char str4[100] = "hello world";    //str1和str2等价,注意在给每一个元素赋值的时候不能用""号    printf("%s\n%s\n%s\n%s\n",str1,str2,str3,str4);}

字符串数组

# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char str[5][5] = {            "abc1",            "abc2",            "abc3",            "abc4",            "abc5",    };    for (int i =0;i<5;i++){        printf("%s\n",str[i]);    }}

字符串输入

  1. 数组字符串输入
# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char str[10]={0};//初始化数组    scanf("%s",str);//初始化字符串    printf("%s",str);}
  1. 指针字符串输入
# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    //char *p = NULL;//0x00000不行,必须让指针指向可读写的内存地址    //char *p = "AAAAA";//常量不可写    char str[10] = {0};    char *p =str;//这里指针存储了可读可写的内存地址    scanf("%s",p);    printf("%s",p);}
  1. 指针与字符串常量
# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    //char str[100];    //str[100]="abc";//str[100]代表了第101个元素    //str="abc"//str是数组名,常量不可修改    char *p = "abc";//p存储了字符串常量的地址    printf("%s\n",p);//打印字符串    printf("%p\n",*p);//打印字符串的地址,这个和其他指针正好相反    char *q;    q="abc";    printf("%s",q);}

其他

# include <stdio.h>//引用函数库# include <stdlib.h>void main(){    char str[10]="abc";    char *p=str;    printf("%p\n",p);//打印指针地址    printf("%c %c %c\n",*(p+0),*(p+1),*(p+2));//指针形式打印每个字符    printf("%c %c %c\n",p[0],p[1],p[2]);//数组指针的形式打印每个字符    printf("%p %p %p\n",p+0,p+1,p+2);//打印每个字符的地址    printf("%s %s %s\n",p,p+1,p+2);//打印指针地址开始的字符串,所以内容需要是地址    *p='x';//可读写内存,可以被赋值    //str="def"//str是常量不可以被赋值    printf("%s\n",p);    char *q ="def";    printf("%c %c %c\n",*(q+0),*(q+1),*(q+2));//%c打印q是无法显示的因为是地址    printf("%c %c %c\n",q[0],q[1],q[2]);    printf("%p %p %p\n",q+0,q+1,q+2);    printf("%s %s %s\n",q,q+1,q+2);    //*q='x';//常量不可读写,不可以被赋值}
原创粉丝点击