C语言学习 --- 字符串以及字符串的处理函数

字符串和字符数组的区别

1、字符数组存储任意字符都可以

2、字符串需要使用字符数组来存储但是结束时至少要有一个结束标志位 --- \0

定义字符串

将26个字母存储成一个字符串。

        char zimu[28]={'A','B','C','D',...,'Z','\0'};('\0'写不写都行,只要空间给够自动补\0)

对于字符串来说可以使用 " " 来表示常量

        (char zimu[27] = "qwertyuiopasdfghjklzxcvbnm" // 相当于'A','B','C','D',...,'Z','\0')

注意:

        若是存储名字时,要注意1个汉字占2 - 3个字节。

字符串空间的读写操作

char zimu[6];

zimu[0] = 'a';

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

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

%s --- 输出 -- char型地址,从给定的地址开始一个字符一个字符输出,遇到\0结束

例一:定义一个数组存储同学姓名

姓名:多个字符 --- 字符串
char names[30];

printf("请输入同学姓名");
scanf("%s",&names);		//scanf("%s",&names[0]);

printf("同学的姓名是: %s\r\n",names);

例二:定义一个数组存储邮箱

#include<stdio.h>

int main(void)
{
    char mail[30];
    printf("请输入你的邮箱:");
    scanf("%s",mail);
    printf("邮箱为:%s\r\n",mail); 
}

字符串的初始化

char zimu[28]={'A','B','C','D',...,'Z','\0'};

char zimu[27] = "qwertyuiopasdfghjklzxcvbnm"

char a[5] = {'a','b'}; //补'\0' '\0'和 0一样

二维数组存储多个字符串

这些字符串长度要一样 --- 按照最长的元素去开空间

存储5个同学的姓名

char names[5][30];

//通过输入的形式输入5个姓名,然后输出

//&names[0][0]         &names[1][0]         &names[2][0]         &names[3][0]         &names[4][0]

// names[0]                 names[1]                 names[2]                 names[3]                 names[4]

例:存储五个人的名字

int i;
char names[5][30];
for(i=0;i<5;i++)
{
    scanf("%s",&names[i]);
}

for(i=0;i<5;i++)
{
    printf("%s",names[i]);
}

字符串处理函数

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);

strcat

功能:

    连接两个字符串

怎么用:

    #include <string.h>

    char *strcat( char *str1, const char *str2);

    函数将字符串str2连接到str1的末端,并返回指针str1

例1:

//函数将字符串str2连接到str1的末端,并返回指针str1
思路:    
    找到第一个字符串的末尾\0的位置,
    将第二个字符串的字符一次放入到第一个字符串的末尾,注意要覆盖\0
    拼接结束要加一个\0;


char name[30];
printf( "Enter your name: " );
scanf( "%s", name );
strcat( name, " the Great" );
printf( "%s\n", name );

例2:

//定义两个字符串a,b,一个用来存储区号,一个用来存储电话号码,输入城市对应的区号和座机号码
//再定义一个数组,将拼接后的总的电话号码放入到数组中。
//输出c

#include <stdio.h>  
#include <string.h>  
  
int main()   
{     
    char a[30];  
    char b[30];   
    char c[30];
  
    printf("请输入区号: ");  
    scanf("%s", a);
    printf("请输入电话号码: ");  
    scanf("%s", b); 
  

  

    strcat(c, a);  
    strcat(c, b);  
   
    printf("%s\n", c);  
  
    return 0;  
}

strcpy

功能:

    拷贝字符串

    当我定义一个字符空间

    需要对这个字符串进行整体赋值 --- 需要拷贝

怎么用:

    #include <string.h>

    char *strcpy( char *to, const char *from );

    char name[30];

    strcpy(names,"Xiaoming");

    复制字符串from 中的字符到字符串to,包括空值结束符。返回值为指针to。

strcmp

功能:

    字符串的比较

不直接比较的原因:

    字符串不能直接用数组名比较

怎么用:

    #include <string.h>

    int strcmp( const char *str1, const char *str2 );

    返回值为指针to

    -1  str1  小于  str2

     0  str1  等于  str2

     1  str1  大于  str2

如何比较两个字符串的大小?

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

int main(void)
{
    int res = strcmp("hello","htty");  // -1
    printf("%d\r\n",res);  // -1
    
    int res1 = strcmp("htty","hello"); // 1
    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); // -1
    return 0;
}

例:设计一个登录程序,验证账号和密码

#include <stdio.h>  
#include <string.h>  
  
int main() {  
    char username[50], password[50];  
    char yuanc[] = "12138";  
    char yuanm[] = "123456";  
   
    printf("请输入用户名: ");  
    scanf("%s", username);  
    printf("请输入密码: ");  
    scanf("%s", password);  
    
    if (strcmp(username, yuanc) == 0 && strcmp(password, yuanm) == 0) 
    {  
        printf("登录成功!\n");  
    } else 
    {  
        printf("用户名或密码错误!\n");  
    }  
  
    return 0;  
}

strstr

功能:

    在一字符串中查找指定的字符串首次出现的位置

怎么用:

    #include <string.h>

    char *strstr( const char *str1, const char *str2 );

    函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL

例:如何查找字符串内的位置?

思路:
    拿第一个字符串的每一个字符和第二个字符串的首字符进行对比。
    
    如果 第一个字符串的某一个字符和第二个字符串的首字符相等
    {
    以第二个串的长度进行对比剩下的字符,多次对比
    {
    如果有一对不一样,结束本次比较
    }
    
    如果上述对比的多次中每一次字符都一样,就找到字符串 结束查找
    }


    char str1[] = "name:lili,age:10";
    char s1[] = "age";
    char s2[] = "score";

    char *p = strstr(str1,s1);

    if(p == NULL)
    {
        printf("没有找到");
    }
    else
    {
        printf("找到了");
    }

strchr

功能1:

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

怎么用:

    #include <string.h>

    char *strchr( const char *str, int ch );

    函数返回一个指向str中ch首次出现的位置,当没有在str中找到ch就返回NULL。

例:同样是寻找问题

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

    char str1[] = "name:lili,age:10";
    char ch;

    scanf("%c",&ch);

    char *p = strchr(str1,ch);

    if(p == NULL)
    {
        printf("没有找到");
    }
    else
    {
        printf("找到了 %p",p);
        printf("%p\r\n",str1);
    }

memset

功能:

    将一段内存空间填入某值

怎么用:

    #include <string.h>

    void *memset( void *buffer, int ch, size_t count );

    函数拷贝ch 到buffer 从头开始的count 个字符里, 并返回buffer指针。 memset() 可以应用在将一段内存初始化为某个值。

    例如:

        memset( the_array, '\0', sizeof(the_array) );

    这是一个将数组的所以分量设置成零的很便捷的方法。

例:memset的使用

int a[5] = {9,3,8,4,100};

//a[1] a[2] 清成0

memset(a[1],'\0',4*2);//其中4*2是指数组中从第一位开始填充八位

int i;
for(i=0;i<5;i++)
{
    printf("%d",a[i]);
}

随机数

#include<stdio.h>

#include <stdlib.h>

#include <time.h>

// 库函数 -- 生成随机数

// 引入库文件

//rand() 返回一个随机数

// #include <stdlib.h> int rand( void ); // 函数声明

// srand() 初始化随机数发生源

// #include <stdlib.h> void srand( unsigned int seed );

//time() 返回系统的当前日历时间

// #include <time.h> time_t time( time_t *time );

// time_t --- 整型

// 类型别名 u8 --- unsigned char

例:

int main(void)
{
    int res;
    
    // 刷新种子  srand
    
    // 用时间戳作为srand的数值,这样每次运行种子就会变化 
    // 时间戳:当前时间距离1970-1-1有多少秒 	
    srand(time(NULL));  // 刷新种子 
    res = rand() % 10;  // rand()生成随机数需要一个种子,种子电脑 启动就生成好了,可能还会有缓存 
    
    printf("%d",res);
    return 0;
}

  • 16
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值