字符串数组及其应用

1、字符串的概念

  • 字符串是有序字符的集合
  • 字符串是程序的基本元素之一
  • C语言中没有字符串的概念
    • C语言中通过特殊的字符数组模拟字符串
    • C语言中的字符串是以**‘ \0 ’**结尾的字符数组
  • 在C语言中,双引号引用的单个或多个字符是一种特殊的字面量
    • 存储于程序的全局只读存储区
    • 本质为字符数组,编译器自动在结尾加上 ‘ \0 ’ 字符
  • 字符串字面量的本质是一个数组
  • 字符串字面量可以看作常量指针
  • 字符串字面量中的字符不可改变
  • 字符串字面量至少包括一个字符
#include <stdio.h>
int main()
{
    char b = "abc"[0];
    char c = *("123" + 1);
    char t = *"";
    
    printf("%c\n", b);//打印 a
    printf("%c\n", c);//打印 2
    printf("%d\n", t);//打印 0
    
    printf("%s\n", "Hello");
    printf("%p\n", "World");
    
    return 0;
}

2、字符串的长度

  • 字符串的长度就是字符串所包含字符的个数
  • 字符串长度指的是第一个**’ \0 '**前出现的字符个数
  • 通过**’ \0 '**结束符来确定字符串的长度
  • 函数 strlen用于返回字符串的长度
char s[] = "Hello";

printf("%d\n", strlen(s));		//打印 5
printf("%d\n", strlen("123"));	//打印 3

3、字符串的输入输出函数

3.1 fgets/gets输入

gets

  • 函数原型char *gets(char *s);
  • 参数:字符串的首地址
  • gets不会检查数组越界,使用时要确保数组足够大

fgets

  • 函数原型char *fegets(char *s, int size, FILE *stream);
  • 参数:字符串首地址,字符串长度,缓冲区

3.2 puts输出

  • 函数原型int puts(const char *s);
  • 作用:终端输出字符串
  • 参数:字符串首地址

4、string函数族

  • 如果想要使用string函数族中的函数,需要导入头文件<string.h>

4.1 strlen

  • 求字符串的实际长度
  • 函数原型size_t strlen(const char *s);
  • 返回值:无符号的长整型,字符串的实际长度
  • 参数:字符串首地址

4.1.1 strlen的自实现

int strlen_r(const char* s)
{
    if( *s )
    {
        return 1 + strlen_r(s+1);
    }
    else
    {
        return 0;
    }
}

4.2 strcpy

  • 复制字符串
  • 函数原型char *strcpy(char *dest, const char *src);
  • 返回值:目标字符串的首地址
  • 参数:char *dest目标字符串
    const char *src源字符串

4.2.1 strcpy的自实现

char* my_strcpy(char* a,const char* b)
{
	char* ret = a;

	while(*b != 0)
	{
		*a++ = *b++;
	}
	return ret;
}

4.3 strcat

  • 拼接字符串
  • 函数原型char *strcat(char *dest, const char *src);
  • 返回值:目标字符串的首地址
  • 参数:char *dest目标字符串
    const char *src要拼接的字符串

4.3.1strcat的自实现

char* my_strcat(char* a,const char* b)
{
	char* ret = a;

	int len = strlen(a);
	a = a + len;

	while(*b != 0)
	{
		*a++ = *b++;
	}
	return ret;
}

4.4 strcmp

  • 字符串比较
  • 函数原型int strcmp(const char *s1, const char *s2);
  • 返回值:ASCII的差值
  • 参数:const char *s1, const char *s2
  • 通过返回值判断,s1和s2的大小关系:
    返回值>0, s1>s2
    返回值=0, s1=s2
    返回值<0, s1<s2

4.4.1 strcmp的自实现

int my_strcmp(char* a,const char* b)
{
	while(*a++ == *b++)
	{
		if(*a == 0)
			return 0;
	}

	return *a > *b ? 1 : -1;
}

5、字符串数组的应用

5.1一维数组中求第二大值

int Secmax(int arr[], int b, int e)
{
	int max = b;
	int sec = 0;
	int i = 0;

	for(i = b; i <= e; i++)
	{
		//找到最大值下标,并把上一个值进行保留
		if(arr[max] < arr[i])
		{
			sec = max;
			max = i;
		}
	}
	return sec;
}

5.2 冒泡排序

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

#define LEN 10

void Sort(int arr[], int len)
{
	int i = 0;
	int j = 0;
	int temp = 0;

	for(i = 0; i < len - 1; i++)
	{
		for(j = 1; j < len - i; j++)
		{
			if(arr[j - 1] > arr[j])
			{
				temp = arr[j - 1];
				arr[j - 1] = arr[j];
				arr[j] = temp;
			}
		}
	}
}

void Print(int arr[], int len)
{
	int i = 0;
	while(i < len)
		printf("%d ", arr[i++]);
	puts("");
}

int main(int argc, const char *argv[])
{
	int arr[LEN] = {};
	int i = 0;

	while(i < LEN)
		scanf("%d", &arr[i++]);
	
	Sort(arr, LEN);

	Print(arr, LEN);

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值