字符串
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;
```
}