字符输入和输出函数
#include <stdio.h>
int main()
{
int c;
c = getchar();
putchar(c);
putchar('\n');
return 0;
}
字符串与字符数组的关系
字符串经常以常量的形式出现
字符串常量的例子:"a + b - c"
字符串以常以字符数组内容的形式出现
字符串常量与一般字符数组中的字符数组不同,
字符串常量在程序内部保存在字符数组中,字符串常量位于特殊的存储区域
字符数组初始化的两种方式
char str[ ] = { 'H', 'e' , 'l' , 'l' , 'o'};
(5个元素,无字符结束符)
(不是一个完整的字符串)
char str[ ] = "Hello";
(6个元素, 自动加上最后一个字符串结束符 '\0')
(是一个完整的字符串)
字符串存储形式
例:"a + b - c"
长度:5
包含字符个数:6
最后一个字符:’\0'
字符串输出函数
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "hello";
puts(a);
puts("world"); /*注意该函数自动换行功能*/
return 0;
}
字符串输入问题
有问题代码,scanf()遇到空格就自动终止输入
scanf()函数详解
函数原型:(int)scanf()
scanf("%d",&x)有三种返回值:
- 返回1表示成功读入一项数据
- 返回0表示读入数据失败
- 返回EOF表示遇到了文件结束(可以用Ctrl-Z或Ctrl-D送入文件结束信息EOF)
从标准输入上读入以空格、tab键或换行符分隔的字符序列
不包含用于分隔各个输入字段的空白符
#include <stdio.h>
int main()
{
char s1[13], s2[13];
scanf("%s\n%s", s1, s2);
printf("%s\n%s\n", s1, s2);
s2[2] = '9'; /*注意9上必须加''(单引号)*/
printf("%s\n", s2);
return 0;
}
正确操作
char *gets(char s[ ]);
读入字符序列,直至换行符或输入数据的结尾
将字符序列保存在一个字符数组中,在输入数据末尾加上字符串结束符 ' \0 '
为防止黑客攻击(缓冲区溢出攻击)用fgets( )代替gets( )
char * fgets ( char *s, int n, FILE *fp);
gets( )与fgets( ) 的区别
- 在输入数据末尾加上字符串结束符 ' \0 '(预留最后一个空格)
- fgets( )最多只读入 n - 1 个字符,以确保任何输入数据都不会造成缓冲区溢出
- 如果缓冲区s足够大,并且输入数据中包含换行符,则该换行符会被保存在缓冲区s中
- 在使用fgets( )时必须指明数据的来源,例如使用标识符stdin说明数据来自标准输入
gets( )函数例子
输入:187 9927 2683
输出:189 9927 2683
#include <stdio.h>
#include <string.h>
int main()
{
char s1[13];
gets(s1);
s1[2] = '9'; /*注意9上必须加''(单引号)*/
puts(s1);
return 0;
}
fgets( )函数例子
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize];
fgets(s1, Maxsize, stdin);
s1[2] = '9'; /*注意9上必须加''(单引号)*/
puts(s1);
return 0;
}
练习
输入:
18799272683
15631468722
输出:
18999272683
15631468722
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize], s2[Maxsize];
gets(s1);
gets(s2);
s1[2] = '9';
puts(s1);
puts(s2);
return 0;
}
输入:
18799272683
15631468722
输出:
1899927268315631468722
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize], s2[Maxsize];
gets(s1);
gets(s2);
s1[2] = '9';
printf("%s", s1);
printf("%s", s2);
return 0;
}
输入:.
187 9927 2683
156 3146 8722
输出:
189 9927 2683
156 3146 87222
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize], s2[Maxsize];
fgets(s1, Maxsize, stdin);
fgets(s2, Maxsize, stdin);
s1[2] = '9'; /*注意9上必须加''(单引号)*/
puts(s1);
puts(s2);
return 0;
}
输入:
187 9927 2683
156 3146 8722
输出:
189 9927 2683
156 3146 8722
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize], s2[Maxsize];
fgets(s1, Maxsize, stdin);
fgets(s2, Maxsize, stdin);
s1[2] = '9'; /*注意9上必须加''(单引号)*/
printf("%s", s1);
printf("%s", s2);
return 0;
}
输入n行字符串
输入:
2
187 9927 2683
181 3210 6429
输出:
187 9927 2683
181 3210 6429
#include <stdio.h>
#include <string.h>
int main()
{
int N;
int i;
scanf("%d\n", &N); /*不要忘记'\n'*/
char s[N][90];
for(i = 0; i < N; i++)
gets(s[i]);
for(i = 0; i < N; i++)
puts(s[i]);
return 0;
}
等价代码
#include <stdio.h>
#include <string.h>
int main()
{
int N;
int i;
scanf("%d\n", &N); /*不要忘记'\n'*/
char s[N][90];
for(i = 0; i < N; i++)
fgets(s[i], 90, stdin);
for(i = 0; i < N; i++)
printf("%s", s[i]);
return 0;
}
字符串逆置函数
#include <stdio.h>
#include <string.h>
#define Maxsize 100
void rev(char s[])
{
int head = 0, rear = 0;
char t;
while(s[rear] != '\0') /*rear不能从-1算起,因为无s[-1]此项!*/
rear++;
for(rear--; head < rear; head++, rear--)
{
t = s[head];
s[head] = s[rear];
s[rear] = t;
}
}
int main()
{
char s[Maxsize] = "Peking University";
puts(s);
rev(s);
puts(s);
return 0;
}
更具有通用性的改进版
#include <stdio.h>
#include <string.h>
#define Maxsize 100
void rev(char s[])
{
int head = 0, rear = 0;
char t;
while(s[rear] != '\0') /*rear不能从-1算起,因为无s[-1]此项!*/
rear++;
for(rear--; head < rear; head++, rear--)
{
t = s[head];
s[head] = s[rear];
s[rear] = t;
}
}
int main()
{
char s[Maxsize];
gets(s);
rev(s);
puts(s);
return 0;
}
字符串复制函数
char *strcpy (char dest[ ], char src[ ] );
char *strncpy(char dest[ ], char src[ ], int n);
dest---destination, src---source
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize];
char s[2][Maxsize];
gets(s1);
strcpy(s[0], s1);
s1[2] = '9';
strcpy(s[1], s1);
int i;
for(i = 0; i < 2; i++)
printf("%s\n", s[i]);
return 0;
}
注意:函数strncpy( )有可能因n太小没有 '\0', 需要时需添加
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char s1[Maxsize];
char s[2][Maxsize];
gets(s1);
strncpy(s[0], s1, 5);
s1[2] = '9';
strcpy(s[1], s1);
int i;
for(i = 0; i < 2; i++)
printf("%s\n", s[i]);
char s2[Maxsize];
strcpy(s2, s[0]);
puts(s2);
return 0;
}
输出:That is nobook
#include <stdio.h>
#include <string.h>
#define Maxsize 100
int main()
{
char d[Maxsize] = "This is a book";
char s[Maxsize] = "That is not his pen";
strncpy(d, s, 10);
puts(d);
}
函数的实现 strcpy( )
char *strcpy(char dest[], char src[])
{
int i = 0;
while(dest[i] = src[i]) ++i; /*(dest[i] = src[i]的值是当前正在复制的字符*/
}
函数的实现strncpy( )
#include <stdio.h>
#include <string.h>
#define Maxsize 100
char *stringncopy(char dest[], char src[], int n)
{
int i;
while(src[i] != '\0') i++;
int k;
if(n <= i)
{
for(k = 0; k < n ;k++)
dest[k] = src[k];
}
else
{
for(k = 0; k <= i; k++)
dest[k] = src[k];
}
return dest;
}
int main()
{
char s1[Maxsize] = "This is a book";
char s2[Maxsize] = "That is not his pen";
stringncopy(s1, s2, 10);
puts(s1);
char s3[Maxsize] = "This is a book";
stringncopy(s2, s3, 10);
puts(s2);
return 0;
}
输出:That is not his p
#include <stdio.h>
#include <string.h>
#define Maxsize 100
char *stringncopy(char dest[], char src[], int n)
{
int i = 0;
while(src[i] != '\0') i++;
int k;
if(n <= i)
{
for(k = 0; k < n ;k++)
dest[k] = src[k];
}
else
{
for(k = 0; k <= i; k++)
dest[k] = src[k];
}
return dest;
}
int main()
{
char s1[Maxsize] = "This is a book";
char s2[Maxsize] = "That is not his pen";
stringncopy(s1, s2, 17);
puts(s1);
return 0;
}
更改一个字符数组的错误做法(不同于数字数组)
char s1[Maxsize] = "This is a book";
char s1[Maxsize] = "File av not cv";
正确做法
char s1[Maxsize] = "This is a book";
strcpy(s1, "File av not cv");
字符串追加函数
函数原型
char *stract(char dest[ ], char src[ ]);
char *strnact(char dest[ ], char src[ ], int n);
字符串前缀删除函数
#include <stdio.h>
#include <string.h>
#define Maxsize 40
void delPrefix(int dnum, char a[])
{
int i = 0;
while((a[i] = a[i+dnum]) != '\0')
i++;
}
int main()
{
char b[13] = "Good morning";
delPrefix(5, b);
puts(b);
return 0;
}
字符串比较函数
int strcmp(char s1[ ], char s2[ ]);
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "hello world";
char b[] = "hello World";
char c[] = "world hello";
printf("a-b:%d\t", strcmp(a,b));
printf("a-b:%d\t", strncmp(a, b, 5));
printf("a-c:%d\n",strcmp(a,c));
return 0;
}
字符串检查函数
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "Hello world";
printf("strlen(a):%d\t", strlen(a));
return 0;
}