C语言入门零基础:9、字符串

一、字符串定义

        1、字符串和字符数组的区别:

                字符数组存任意数组都可以,它可以以任何字符结尾;

                字符串需要使用字符数组来存,但是结束必须要有一个'\0'字符;

                只有字符串才能用双引号定义

        2、字符串定义格式

                将26个字母存储成一个字符串。
        
                (1) char zimu[27] = {'A','B','C','D','E'。。。。。'Z','\0'};
        
                (2) char zimu[27] = "abc....z"; //结束时要预留一个空间,系统会自动补一个'\0'

                          即实际存储的是'a','b','c',....'Z','\0'。

        3、字符串的输入输出

                字符串可以整体的输入和输出  

                格式控制符 :%s 
                输入 : char型地址,从给定的地址开始一个字符一个字符放入到内存空间中,遇到空格或者回车结束的时候会给你在后面补个'\0'
                输出 :char型地址, 从给定的地址开始 一个字符一个字符输出,遇到'\0'结束

注意:用%s时不论是输出还是输入都要传地址

二、字符串处理函数

        1、strlen


                功能:返回指定字符串的长度 
        
                怎么用:
                    #include <string.h>
                    size_t strlen( char *str );
            
                    函数返回字符串str 的长度( 即空值结束符\0之前字符数目)。
            
                用法:
                    输入一个字符串,输出这个字符串对应的长度
            
                    char str[30];
                    printf("请输入字符串");
                    scanf("%s",str);
            
                    int res = strlen(str);
                    printf("%d",res);

                

        可以看到我输入了123456,输出为6个字符

        2、strcat

                功能:连接两个字符串 
        
                怎么用:
                    #include <string.h>
                    char *strcat( char *str1, const char *str2 );
            
                    函数将字符串str2 连接到str1的末端,并返回指针str1. 
        
                使用:
                    #include<stdio.h>
                    #include<string.h>

                    int main(void)
                    {
                        char name[30];
                        printf( "Enter your name: " );
                        scanf( "%s", name );
                
                        strcat( name, " the Great" );    //  " the Great"  常量字符串 
                        printf( "%s\n", name );
                        return 0;
                    }
            
        思路:
            找到第一个字符串末尾\0的位置,
            将第二个字符串的字符一次放入到第一个字符串的末尾,注意要覆盖'\0'
            拼接结束要加一个'\0'

        3、strcpy

        功能: 拷贝字符串 ,原因:字符串不能直接用数组名赋值
        
        当定义一个字符串空间;        
        需要对这个字符串进行整体赋值时,只能一个一个的写入,用这个函数就解决了这个问题。
        

        用法:
            char names[30];
            strcpy(&names[2],"xiaoming");//这个语句是将xiaoming这个字符串拷贝到names[2]中去
            
            printf("%s",names);
            
        思路:
            读取from位置开始的每一个字符放入到to对应的空间里面去,直到遇到from字符串里面\0位置,结束的时候要在to的后面补\0

        4、strcmp

        功能:字符串的比较  原因:字符串不能直接用数组名比较
            

                    返回值为指针to。
                        -1             str1 小于  str2
                        0              str1 等于  str2
                        1              str1 大于  str2
                
                两个字符串如何比较大小?        

        代码:
                #include<stdio.h>
                #include<string.h> 

                int main(void)
                {
                    int res = strcmp("hello","htty"); 
                    printf("%d\r\n",res);  // 由于hello这个字符串与htty这个字符串大小不一致并且hello长度长一些,所以输出-1
                    
                    int res1 = strcmp("htty","hello");
                    printf("%d\r\n",res1); // 1
                    
                    
                    int res2 = strcmp("hello","hello");
                    printf("%d\r\n",res2); // 0
                    
                    int res3 = strcmp("hello","hello world");
                    printf("%d\r\n",res3); // 在hello world中遇到空格直接结束查找,而前面的hello多一个'\0'字符,所以输出-1
                    return 0;
                }

        5、strstr

        功能:在一字符串中查找指定的子串首次出现的位置 --- 在一个字符串里面查找另外一个子串
           

     代码:

            #include<stdio.h>
            #include<string.h>

            int main(void)
            {
                char str1[] = "name:lili,age:10";
                char s1[] = "age";            
                char s2[] = "score";
                
                char *p = strstr(str1,s2);
                
                if(p == NULL)
                {
                    printf("没有找到");
                }
                else
                {
                    printf("找到了");
                }
                return 0;
            }

        6、strchr   

        功能:查找某字符在字符串中首次出现的位置 

        代码:
            #include<stdio.h>
            #include<string.h>

            int main(void)
            {
                char str1[] = "name:lili,age:10";
                char ch;
                scanf("%c",&ch);
                
                char *p = strchr(str1,ch);
                
                if(p == NULL)
                {
                    printf("没有找到");
                }
                else
                {
                    printf("找到了");
                }
                return 0;
            }

        思路:从给定的地址开始匹配字符,匹配上就可以结束了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值