C语言基础 -30 数组_字符数组输入与输出常用函数

book@100ask:~/C_coding/CH01$ cat char1.c
#include <stdio.h>
#include <stdlib.h>

#define N 32

int main()
{
	char str[N] = {'a','b','c'};
	int i;
	scanf("%s",str); 
	printf("%s\n",str);
	exit(0);
}


book@100ask:~/C_coding/CH01$ make char1
cc     char1.c   -o char1
book@100ask:~/C_coding/CH01$ ./char1
hello
hello 
book@100ask:~/C_coding/CH01$ 
book@100ask:~/C_coding/CH01$ cat char1.c
#include <stdio.h>
#include <stdlib.h>

#define N 32

int main()
{
	char str[N],str1[N],str2[N];
	int i;
	scanf("%s%s%s",str,str1,str2); 
	printf("%s\n%s\n%s\n",str,str1,str2);
	exit(0);
}


book@100ask:~/C_coding/CH01$ make char1
cc     char1.c   -o char1
book@100ask:~/C_coding/CH01$ ./char1
hello word chaina
hello
word
chaina
book@100ask:~/C_coding/CH01$ ./char1
hello he2 he3 he4 he5 he6   //当有多个输出时,自动只赋值前三个串
hello
he2
he3

字符串常用函数:

strlen & sizeof

book@100ask:~/C_coding/CH01$ cat char2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// strlen & sizeof 
#define N 32

int main()
{
	char str[] = "hello";
	printf("%d\n",strlen(str));   //strlen显示字符的实际长度,此处hello包含5个字符,故显示5
	printf("%d\n",sizeof(str));  //sizeof打印字符串的总长度,包括尾零,此处hello包括尾零共6
	exit(0);
}
book@100ask:~/C_coding/CH01$ make char2
cc     char2.c   -o char2
book@100ask:~/C_coding/CH01$ ./char2
5
6
book@100ask:~/C_coding/CH01$ cat char2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 32

int main()
{
	char str[] = "hello\0abc";
	printf("%d\n",strlen(str)); //strlen以\0分割,因此此处打印5
	printf("%d\n",sizeof(str)); //sizeof会将中间的\0也算进去,算一个空间,hello + \0 + abc + \0 = 10个字符空间
	exit(0);
}

book@100ask:~/C_coding/CH01$ ./char2
5
10

strcpy & strncpy

book@100ask:~/C_coding/CH01$ cat strcp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 32

int main()
{
	char str[] = "hello\0abc";  //给字符串数组str赋初值
	strcpy(str,"abcde");  //将abcde copy给字符串数组str
	puts(str);            //将strcp运算后的字符串str输出
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strcp
cc     strcp.c   -o strcp
book@100ask:~/C_coding/CH01$ ./strcp
abcde
book@100ask:~/C_coding/CH01$ cat strcp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 32

int main()
{
	char str[3];          //初始化,定义字符串数组的长度为3
	strcpy(str,"abcde");   //当copy的内容超过容量,仍然可以赋值,即字符串数组长度的定义在此处失效
	puts(str);
	exit(0);
}
book@100ask:~/C_coding/CH01$ make strcp
cc     strcp.c   -o strcp
book@100ask:~/C_coding/CH01$ ./strcp
abcde
book@100ask:~/C_coding/CH01$ cat strncp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE];
	strncpy(str,"abcde",STRSIZE);   //限定copy的最大字符数为STRSIZE
	puts(str);
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strncp
cc     strncp.c   -o strncp
book@100ask:~/C_coding/CH01$ ./strncp
abcde
book@100ask:~/C_coding/CH01$ cat strncp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 3

int main()
{
	char str[STRSIZE];
	strncpy(str,"abcde",STRSIZE);   //当限定copy的最大字符数为STRSIZE=3时,最多可被copy3个字符
	puts(str);
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strncp
cc     strncp.c   -o strncp
book@100ask:~/C_coding/CH01$ ./strncp
abc

 strcat & strncat

book@100ask:~/C_coding/CH01$ cat strcat.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE] = "hello";
	strcat(str," ");               //strcat负责把不同的字符串连接在一起
	strcat(str,"world!");
	puts(str);
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strcat
cc     strcat.c   -o strcat
book@100ask:~/C_coding/CH01$ ./strcat
hello world!
book@100ask:~/C_coding/CH01$ cat strnc
strncat    strncat.c  strncp     strncp.c   
book@100ask:~/C_coding/CH01$ cat strncat.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 5

int main()
{
	char str[STRSIZE] = "hello";
	strncat(str," ",STRSIZE);
	strncat(str,"world!",STRSIZE);   //限定一次连接的最大长度为STRSIZE
	puts(str);
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strncat
cc     strncat.c   -o strncat
book@100ask:~/C_coding/CH01$ ./strncat
hello world

 strcmp & strncmp

book@100ask:~/C_coding/CH01$ cat strcmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE] = "hello";
	char str1[STRSIZE] = "world";
	printf("%d\n",strcmp(str,str1));    //对比,左边的字符串(从第一个字符开始比较,大于则返回整数,小于返回负数)
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strcmp
cc     strcmp.c   -o strcmp
book@100ask:~/C_coding/CH01$ ./strcmp
-15
book@100ask:~/C_coding/CH01$ cat strcmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE] = "hello";
	char str1[STRSIZE] = "aworld";
	printf("%d\n",strcmp(str,str1));
	exit(0);
}

book@100ask:~/C_coding/CH01$ make strcmp
cc     strcmp.c   -o strcmp
book@100ask:~/C_coding/CH01$ ./strcmp
7
book@100ask:~/C_coding/CH01$ cat strcmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE] = "hello";
	char str1[STRSIZE] = "hello";
	printf("%d\n",strcmp(str,str1));
	exit(0);
}


book@100ask:~/C_coding/CH01$ make strcmp
cc     strcmp.c   -o strcmp
book@100ask:~/C_coding/CH01$ ./strcmp
0
book@100ask:~/C_coding/CH01$ cat strcmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE] = "hello";
	char str1[STRSIZE] = "helloa";
	printf("%d\n",strcmp(str,str1));
	exit(0);
}

book@100ask:~/C_coding/CH01$ make strcmp
cc     strcmp.c   -o strcmp
book@100ask:~/C_coding/CH01$ ./strcmp
-97

book@100ask:~/C_coding/CH01$ cat strncmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 32

int main()
{
	char str[STRSIZE] = "hello";
	char str1[STRSIZE] = "helloa";
	printf("%d\n",strncmp(str,str1,5));  //strncmp设定对比的字符数为前n个
	exit(0);
}

book@100ask:~/C_coding/CH01$ make strncmp
cc     strncmp.c   -o strncmp
book@100ask:~/C_coding/CH01$ ./strncmp
0

猜你喜欢

转载自blog.csdn.net/f2157120/article/details/106795077