zoukankan      html  css  js  c++  java
  • C语言入门-字符串

    还是要继续学习,每天给自己打气。

    字符数组

    char word[] = {'H','e','l','l','o'};
    

    这不是c语言的字符串,不能用字符串的方式做计算

    一、字符串

    char word[] = {'H','e','l','l','o',''};
    

    区别就是最后多了一个0,这就成为了字符串

    1. 以0(整数0)结尾的一串字符
    2. 0或''是一样的,但和'0'不同
    3. 0标志字符串的结束,但它不是字符串的一部分
    4. 计算字符串长度的时候不包含这个0
    5. 字符串以数组的形式存在,以数组或者指针的形式访问,更多的是以指针的形式
    6. string.h里有很多处理字符串的函数

    字符串变量

    char *str = "hello";
    char word[] = "hello";
    char line[10] = "hello";
    

    字符串常量

    1. “hello”
    2. “hello"会被编译器变成一个字符数组放在某处,这个数组的长度是6,结尾还有表示结束的0
    3. 两个相邻的字符串常量会被自动连接起来

    字符串

    1. c语言的字符串是以字符数组的形态存在的
    2. 不能用运算符对字符串做运算
    3. 通过数组的方式可以遍历字符串
    4. 唯一特殊的地方是字符串字面量可以用来初始化字符数组

    二、字符串常量

    char *s = "hello world";
    
    1. s是一个指针,初始化为指向一个字符串常量
    2. 由于这个常量所在的地方,所有实际上s是const char* s ,但是由于历史的原因,编译器接受不带const的写法
    3. 但试图对s所指的字符串做写入会导致严重的后果
    4. 如果需要修改字符串,应该用数组
    char s[] = "hello , world!";
    

    指针还是数组

    char *str = "hello";
    char word[] = "hello";
    

    数组:可以读,可以写

    指针:只能读,不能修改

    如果要构造一个字符串 ------>数组

    如果要处理一个字符串 ------>指针

    char*是字符串

    1. 字符串可以表达为char*的形式
    2. char*不一定是字符串
    3. 本意是指向字符串的指针,可能指向的是字符的数组
    4. 只有它所指的字符数组的结尾有0,才能说它所指的是字符串
    #include <stdio.h>
    
    int main(void)
    {
    	char *s = "hello world";
    	char *s1 = "hello world";
    	char s3[] = "hello world";
    
    	// 这个是不能修改的
    	// s[0] = 'B';
    
    	s3[0] = 'B';
    
    	printf("%p
    ", s);
    	printf("%p
    ", s1);
    	printf("%p
    ", s3);
    
    	printf("Here! s[0]=%c
    ", s[0]);
    	printf("Here! s3[0]=%c
    ", s3[0]);
    
    // 	0000000000404000
    // 0000000000404000
    // 000000000062FE30
    // Here! s[0]=h
    // Here! s3[0]=B
    
    	return 0;
    }
    

    三、字符串的输入和输出

    字符串赋值

    char *t = 'title';
    char *s;
    s = t;
    

    并没有产生新的字符串,只是让指针s指向了t所指的字符串,对s的任何操作就是对t做的

    字符串输入和输出

    char string[8];
    scanf("%s" , string);
    printf("%s" , string);
    

    scanf读入一个单词(到空格、tab、回车为止)

    scanf是不安全的,因为不知道要读入的内容的长度

    常见错误

    1. char *string;
    2. scanf("%s" , string);
    3. 以为char*是字符串类型,定义了一个字符串类型的变量string就可以直接使用了
    4. 由于没有对string初始化0 , 所以不一定每次运行都出错

    四、单字符输入和输出

    int putchar(int c);
    
    1. 向标准输出写一个字符
    2. 返回写了几个字符,EOF(-1)表示写失败
    int getchar(void);
    
    1. 从标准输入读入一个字符
    2. 返回类型是int是为了返回EOF(-1) windows --->Ctrl-z

    五、字符串函数strlen

    1. size_t strlen(const char *s);
    2. 返回s的字符串长度(不包括结尾的0)
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char const *argv[])
    {
    	char line[] = "hello";
    
    	printf("strlen=%lu
    ", strlen(line));
    	
    	printf("sizeof=%lu
    ", sizeof(line));
    	return 0;
    }
    //strlen=5
    // 这个包括了字符串最后的0
    //sizeof=6
    
    

    当然了,也可以自己写一个函数来计算字符串长度

    #include <stdio.h>
    #include <string.h>
    
    // 自定义方法来计算字符串长度
    int mylen(const char *s)
    {
    	int idx = 0;
    	while ( s[idx] != '' ){
    		idx++;
    	}
    	return idx;
    }
    
    
    int main(int argc, char const *argv[])
    {
    	char line[] = "hello";
    
    	printf("strlen=%lu
    ", mylen(line));
    	
    	printf("sizeof=%lu
    ", sizeof(line));
    	return 0;
    }
    

    六、字符串函数strcmp

    1. int strcmp(const char *s1 , const char *s2);
    2. 比较两个字符串返回:
      • 0:s1 == s2
      • 1: s1 > s2
      • -1: s1 < s2

    七、字符串函数strcpy

    1. char* strcpy(char *restrict dst , const char * restrict src);
    2. 把src的字符串拷贝到dst
    3. restrict表明src和dst不重叠(c99)
    4. 返回dst,为了能连起代码来

    复制一个字符串

    	char *dst = (char*)malloc(Strlen(src)+1);
    	strcpy(dst , src);
    
    

    八、字符串strcat

    1. char* strcat(char *restrict s1 , const char *restrict s2);
    2. 把s2拷贝到s1的后面,接成一个长的字符串
    3. 返回s1
    4. s1必须具有足够的空间

    但是这些函数都是不安全的,因为你不知道有没有足够的空间,所以

    安全版本:如图

    安全版本.png

    九、字符串搜索函数

    1. char * strchr(const char *s , int c);
    2. char * strrchr(const chat *s int c);
    3. 返回NULL表示没有找到
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char const *argv[])
    {
    	char s[] = "hello";
    
    	char *p = strchr(s, 'l');
    	// 找到第二个l
    	p = strchr(p+1,'l');
    	printf("%s
    ", p);
    
    	return 0;
    }
    

    复制给另一个

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char const *argv[])
    {
    	char s[] = "hello";
    	char *p = strchr(s, 'l');
    	char *t = (char*)malloc(strlen(p)+1);
    	strcpy(t,p);
    	printf("%s
    ", t);
    	free(t);
    	// llo
    	return 0;
    }
    
  • 相关阅读:
    一些想说的事
    化学离子平衡作业偷懒神器
    solution
    SGU 刷题记
    INT128
    # 字典树的指针写法 1.
    CSP-S2 游记
    Tarjan 【整理】
    HGOI 20191106
    20191101
  • 原文地址:https://www.cnblogs.com/mengd/p/11625954.html
Copyright © 2011-2022 走看看