C---字符串的处理函数

用于输入输出的字符串函数,在使用前应包含头文件“stdio.h”,使用其他字符串函数则应包含头文件“string.h”

1.字符串输入函数gets()

调用形式是gets(字符数组)

功能:从键盘输入一个字符串(包括空格)赋给从字符串数组起始的存储单元中,直到读入一个回车符为止。回车符读入后,不作为字符串的内容,系统将自动用'\0'替换,作为字符串结束的标志。

2.字符串输出函数puts()

调用形式puts(字符数组)

功能:将字符数组起始地址开始的一个字符串(以'\0'结束的字符序列)输出到显示器,并将字符串结束标志'\0'转换成'\n',自动输出一个换行符。

例如:char c[]="How\nare\nyou!";

    puts(c)

输出结果:

      How

      are

      you!

 1 //字符串输入gets()函数,字符串输出puts()函数
 2 #include <stdio.h>
 3 void main ()
 4 
 5 {
 6     char s[5];
 7     printf("input string:\n");
 8     gets(s);
 9     puts(s);
10 
11 
12 }
puts()—gets()

 3.字符串长度函数strlen()

调用形式为:strlen(字符数组或字符串)

功能:测试字符数组起始地址开始的字符串(以'\0'结束的字符序列)有效长度。函数值为字符数组或字符串的有效个数,但不包括'\0'在内。

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 void main()
 4 {
 5     int k,m;
 6     char c1[20]="How\nare\nyou!";
 7     char c2[  ]="C language";
 8     k=strlen(c1);
 9     m=strlen(c2);
10     printf("The length of the string are %d\n%d\n",k,m);
11 
12 }
strlen

 

结果:

 

posted on 2022-01-24 11:36  GRIT_风  阅读(95)  评论(0)    收藏  举报