字符串(C语言)

字符串

1.字符串基础:

​ 字符串(字符串字面值)是用双引号包含的字符序列。

​ NULL:终止字符串的符号常量,转义序列符是’\0’。

​ 数组中的单个字符能够利用下标或指针符的标准数组处理技术来进行输入,输出。

2.字符串的输入和输出

​ 单个字符输入:getchar() 输出:putchar()

​ 多个字符输入:gets() 输出:puts()

字符串赋值strcopy():

/*
	Name: 
	Copyright: 
	Author: 
	Date: 29/02/20 23:15
	Description: 字符串的复制 
*/
#include<stdio.h>
#define M  100
void strcopy(char new_message[],char message[]);
int main(){
	char message[M];
	char new_message[M];
	printf("please input the string:\n");
	gets(message);
	strcopy(new_message,message);
	puts(new_message);
	
	return 0;
}

void strcopy(char new_message[],char message[]){
	/*  数组形式 
	int i=0;
	while(message[i]!='\0'){
		new_message[i]=message[i];
		i++;
	}
	new_message[i]='\0';
	*/
    //  指针操作 
    char *p,*q;
    p=message;
    q=new_message;
    while(*q=*p){
    	p++;
    	q++;
	}
}

计算字符个数strcount():

/*
​	Name: 
​	Copyright: 
​	Author: 
​	Date: 29/02/20 23:39
​	Description: 字符计数 
*/
#include<stdio.h>
#define M  66
int count_words(char list[]);
int main(){
​	char list[M];
​	printf("input these words:\n");
​	gets(list);
​	count_words(list);
​	printf("the list %d big!",count_words(list));
​	

​```
return 0;
​```

} 

int count_words(char list[]){
​	int count=0;
​	int i=0;
​	while(list[i]!='\0'){
​		i++;
​		count++;
​	}
​	return count;
}

3.使用指针处理字符串

/*
​	Name: 
​	Copyright: 
​	Author: 
​	Date: 29/02/20 23:53
​	Description: 数组指针 
*/
#include<stdio.h>
int main(){
​	int n;
​	char *season[]={"spring","summer","fall","winter"};
​	for(n=0;n<4;n++){
​		//season[n] 存储着字符串的首地址 
​		printf("%s\n",season[n]);
​	}
​	

​```
return 0;
​```

} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值