学习C语言 7.31 字符串函数

一、字符串函数

1.strlen

需要加上头文件<string.h>
本体: size_t strlen(const char *s);
功能:  计算字符串长度 
参数:s //表示需要一个指针
         可以给的参数形式
            a.数组名 
            b.字符串常量 
返回值:返回字符串长度

首先我们要弄懂字符串长度和数组长度
例如:char s[] = "hello"; 
s的长度,也就是数组的长度 6 
char s[10] = "hello";
s的长度 ---数组的长度 10 

'h' 'e' 'l' 'l' 'o' '\0' //
字符串长度:'\0' 前面字符个数
"hello" 字符串长度 5个 
它所占的内存空间的字节数是 6 个

strlen计算的就是字符串'\0'前的长度

用代码实现strlen的功能:

#include <stdio.h>

int main(void)
{
	char s[20];
	gets(s);

	int i=0;
	while(s[i]!='\0')
	{
		i++;
	}

	printf("%d\n",i);

	return 0;

}

使用strlen:

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

int main(void)
{
	char s[20];
	gets(s);

	printf("%ld\n",strlen(s));
	printf("%ld\n",strlen("world"));
	return 0;

}

注意:在64位编译器中strlen计算出的类型是长整型,需要使用%ld

2.strcpy

本体:char * strcpy(char *dest, const char *src);
功能:复制字符串
          将src中字符串,复制到dest 
参数:
  src   // 表示原字符串 
          可以给的实参形式:
                  a.一维字符数组名
                  b.字符串常量           
  dest  // 表示目的字符串
         可以给的实参形式:
                  一维字符数组名

通过代码实现strcpy的功能:

#include <stdio.h>

int main(void)
{
	char a[100];
	char b[100];
	gets(a);

while(a[i]!='\0')
	{
		b[i]=a[i];
		++i
	}
	b[i]='\0';

#if 0
    while(b[i]=a[i])
	{
		++i;
	}//同样能实现把a中的字符串复制到b数组中
#endif

	printf("b=%s\n",b);

	return 0;
}

使用strcpy:

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

int main(void)
{
	char a[100];
	char b[100];
	gets(a);

	strcpy(b,a);
	printf("b = %s\n",b);
	
	strcpy(b,"china");
	printf("b = %s\n",b);

	return 0;
}

同样也需要加上头文件<string.h>

3.strcat

本体:char *strcat(char *dest, const char *src);
功能:拼接字符串 
参数:
      dest  //目的字符串 
            可以使用的实参形式
                    一维字符型数组名 
      src   //源字符串 
             可以使用的实参形式
                    a.一维字符型数组名  
                    b.字符串常量

通过代码实现strcat的功能

#include <stdio.h>

int main(void)
{
	char a[100];
	char b[]="world";

	gets(a);

	int j=0;
	while(a[j]!='\0')
	{
		j++;
	}

	int i=0;
	while(b[i]!='\0')
	{
		a[j+i]=b[i];
		i++;
	}
    a[i]='\0';

	puts(a);

	return 0;
}

使用strcat:

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

int main(void)
{
	char a[100];
	char b[]="world";

	gets(a);

    strcat(a,b);

    puts(a);

	return 0;
}

如果dest目的字符串为字符串常量的话会出现“Segmentation fault (core dumped)”,这是段错误,也就是意味着访问到了不可访问的位置,因为常量是不能被修改的。

4.strcmp

本体:int strcmp(const char *s1, const char *s2);
功能:比较字符串大小 
参数:
  s1  //字符串 
  s2  //字符串 
s1、s2都可为数组名、字符串常量 
     
返回值: //最后 停的位置上字符的差值 来反映大小 

通过代码实现strcmp功能:首先需要了解字符串比较规则  

对两个字符串自左至右逐个字符相比(按 ASCII 码值大小比较),直到出现不同的字符或遇到'\0' 为止。如全部字符相同,则认为相等;若出现不相同的字符,则以第1个不相同的字符的比较结果为准。 

比较的结束的条件:
s1[i]!=s2[i] || s1[i]=='\0' || s2[i] == '\0'

只看,结束时,s1 最后一个字符 和 s2最后一个字符 之间的大小关系 
"hello"<"help"
-----------
"hello">"hell"  
----------------
"hello"=="hello"

 代码:

#include <stdio.h>

int main(void)
{
	char a[100];
	char b[100];

	gets(a);
	gets(b);

	int i=0;
	while(a[i]==b[i]&&a[i]!='\0'&&b[i]!='\0')
	{
		++i;
	}
	
	if(a[i]-b[i]>0)
	{
		printf("max = %s\n",a);
	}
	else if(a[i]-b[i]<0)
	{
		printf("max = %s\n",b);
	}
	else
	{
		printf("%s == %s\n",a,b);
	}

	return 0;
}

使用strcmp:

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

int main(void)
{
	char a[100];
	char b[100];

	gets(a);
	gets(b);

	int ret;
	ret = strcmp(a,b);

	printf("%d\n",ret);
	return 0;
}

返回值为正说明a>b,为负说明a<b,为0说明相等。


字符串相关函数:
1.gets
2.puts 
3.strlen 
4.strcpy / strncpy        //n是一个数字,表示复制几个字符
5.strcat / strncat 
6.strcmp / strncmp 

二、二维数组

二维数组:int a[3][4]
   类型说明符 数组名[常量表达式][常量表达式]
     (1) 类型说明符 --- 用来说明数组中存储数据的类型
     (2) 数组名     --- 标识符 
     (3) 常量表达式 --- 行 --多少行
     (4) 常量表达式 --- 列 --多少列 
eg:
   int a[3][4]; //表示说,定义了一个包含有3行 4列 总共12个 int型元素的二维数组 


数组初始化:
  int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};//全部初始化                 
  int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};//全部初始化                
  int a[3][4] = {1,2,3,4,5,6};//部分初始化     
  int a[3][4] = {{1,2},{5,6},{9,10}};//全部初始化
 
  循环赋值:  scanf输入值 

#include <stdio.h>

int main(void)
{
	int a[3][3]={0};

	int i=0;
	int j=0;
	for(i=0;i<3;++i)
	{
		for (j=0;j<3;++j)
		{
			scanf("%d",&a[i][j]);
		}
	}

	for(i=0;i<3;++i)
	{
		for(j=0;j<3;++j)
		{
			printf("%d ",a[i][j]);
		}
        putchar('\n');
	}//循环打印,格式为3行3列

	return 0;
}


 
 二维数组的本质:

 int a[4]; //一维数组 
            //数组也是一种数据类型 
           //这个一维数组的类型是int[4]
           
 将int a[3][4]变为 类型说明符 数组名[常量表达式]; 这种格式就是 int[4]  a [3];  但是c语言不支持这样写。

    int[4] a[3]; // 一维数组 
                 // 这个一维数组的元素 又一个一维数组类型    
                 
    +-------+
    |  a[0]  | //int [4],也就是每个格子里面还放着4个int型元素
    +-------+
    |  a[1]  |
    +-------+
    |  a[2]  |
    +-------+
    int a[3];


注意:
    1.c语言中,并不存在真正的二维数组,都是用一维数组模拟的。
    2.二维数组的本质:实际上 一维数组类型的一维数组      //数组的数组        
    3.二维数组数据存储时,按行优先存储 
    4.多维数组
    int a[3][4][5]; //三维数组 --- 是二维数组类型的一维数组 
    int[4][5] a[3] //n维数组 --- n-1维数组类型的 一维数组 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值