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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章