——黑馬程序員——C語言中字符串處理函數

-----------android培訓java培訓、java學習型技術博客、期待與您交流!-----------

C語言中字符串處理函數


一、puts函數

作用:輸出字符串的函數,將一個字符串(以’\0‘結束的字符序列)輸出
一般形式:
puts(字符數組的地址)
特點:1、用puts函數輸出的字符串中可以包含轉義字符,
   2、在輸出時將結束標誌符'\0'轉換成了'\n',即輸出完之後直接換行
   3、可以是字符數組的首地址,也可以是字符數組元素的地址
   4、必須是字符數組
   5、不可以格式化輸出

二、gets函數

作用:從鍵盤上接收一個字符串的函數,將一個字符串輸入到字符數組中,並得到一個函數值,該函數值就是字符數組的首地址
一般形式:
gets(字符數組的地址)
特點:1、xcode下使用gets時會給出一個警告
   2、不安全:當指定的數組長度小於輸入的字符時不會提示,會存在越界問題
   3、使用gets可以接收空格
測試代碼:
#include <stdio.h>
#include <stdlib.h>
int main()
{
	//定義字符數組
	char s[]={'0'};
    //輸出字符串
    printf("請輸入一個字符串\n",s);
    //接受鍵盤輸入的值
    gets(s);
    //打印輸入的字符串
	puts(s);    
   
	system("pause");
	return 0;
}
測試結果


三、puts、gets函數   putchar、getchar函數和fgets、fputs函數的區別;

1、puts、gets函數用於字符串的輸出和輸入,使用時會有警告,提示不安全,當輸入的字符串長度超出字符數組的長度時會把所有字符串都存到字符數組中去,導致字符串中沒有結束符\0,因此不安全;而putchar、getchar函數用於單個字符,
2、他們都可以輸出轉義字符,
3、putchar、getchar函數後面可以是字符常量、整型常量、字符變量或者整型變量,只要是值在字符的ASCII的範圍之內就可以
4、getchar函數的返回值就是輸入的字符,而gets函數返回值是字符串的地址
5、fgets函數是文件操作相關的函數,他是安全的,當輸入的字符串的長度大於數組的長度,此時fgets會自動的把數組的最後一個元素變成\0存儲
6、當輸入的字符串長度小於數組的長度時,fgets會自動換行
7、fgets函數的一般形式爲:fgets(數組名,數組長度,stdin(標準輸入));不能以格式化輸入
8、fputs函數不會自動換行,不能進行格式化的輸出

四、strcat函數—字符串連接函數

一般形式:strcat(字符數組1,字符數組2)
作用:將字符串2連接到字符串1的後面,結果存放到字符串1當中,函數調用完後得到的是字符數組1的首地址
注意:字符數組1的長度要足夠長,否則會出現溢出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "Hei Ma";
    char s2[] = "Xun Lian Ying";

    //連接字符串 
	strcat (s1,s2);
    //輸出字符串
    printf("連接之後爲:%s\n",s1);
    printf("%s\n",s2);

	system("pause");
	return 0;
}

五、strcpy和strncpy函數—字符串複製函數

一般形式:
strcpy(字符數組1,字符數組2)
作用:將字符數組2複製到字符數組1中去
注意:1、字符數組1必須足夠大,能夠容納被複制的字符數組2,
   2、字符數組1必須寫成數組名形式(如str),字符數組2 可以是字符數組名,也可以是一個字符串常量,例如 strcpy(str,”heima“);
   3、複製時是將字符數組2中的字符串和最後的\0一起復制到字符數組的1中,從字符數組1的第一個元素開始覆蓋,字符數組1被覆蓋完後\0之後的元素是字符數組1中原有的字符
   4、不能將一個字符串常量或者字符數組直接給一個字符數組 如:str = ”HeiMa“;  str = str1; 這樣的賦值是不合法的錯誤的
測試代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "Hei Ma";
    char s2[] = "Xun Lian Ying";

    //複製字符串 
	strcpy (s1,s2);
    //輸出字符串
    printf("複製之後爲");
	//輸出字符數組1
    puts(s1);
    //輸出字符數組2
    puts(s2);

	system("pause");
	return 0;
}
測試結果:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "Hei Ma";
    //char s2[] = "Xun Lian Ying";

    //複製字符串 
	strcpy (s1,"Hei Ma Xun Lian Ying");
    //輸出字符串
    printf("複製之後爲");
	//輸出字符數組1
    puts(s1);
    //輸出字符數組2
    //puts(s2);

	system("pause");
	return 0;
}

strncpy函數:作用是將字符數組2中的前面n個字符複製到字符數組1中去,複製的字符個數n不應該大於字符數組1中原有的字符個數(不包括\0)
一般形式:
strncpy(字符數組1,字符數組2,n);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "Hei Ma";
    char s2[] = "Xun Lian Ying";

    //複製字符串 
	strncpy (s1,s2,2);
    //輸出字符串
    printf("複製之後爲");
	//輸出字符數組1
    puts(s1);
    //輸出字符數組2
    puts(s2);

	system("pause");
	return 0;
}

六、strcmp函數—字符串比較函數

作用:將字符串1和字符串2的自左向右逐個字符按ASCII碼值得大小相比較,直到出現不同的字符或遇到'\0'位止
特點:1、如全部字符相同,則認爲兩個字符串相等
2、若出現不相同的字符,則以第1對不同的字符的比較結果爲準
 如果字符串1 = 字符串2,則函數值爲0
 如果字符串1 > 字符串2,則函數值爲一個正整數
如果字符串1 < 字符串2,則函數值爲一個負整數
3、如果參加比較的兩個字符串都由英文字母組成,則在英文字典中位置在後面的爲大
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "abcd";
    char s2[] = "abcde";

    //複製字符串 
	int x = strcmp (s1,s2);
    //輸出字符串
    printf(" 比較結果爲:%d\n",x);
	//輸出字符數組1
    //puts(s1);
    //輸出字符數組2
    //puts(s2);

	system("pause");
	return 0;
}

int main()
{
	//定義字符數組
	char s1[] = "abcd";
    char s2[] = "abcd";

    //複製字符串 
	int x = strcmp (s1,s2);
    //輸出字符串
    printf(" 比較結果爲:%d\n",x);
	//輸出字符數組1
    //puts(s1);
    //輸出字符數組2
    //puts(s2);

	system("pause");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "heima";
    char s2[] = "Zeima";

    //複製字符串 
	int x = strcmp (s1,s2);
    //輸出字符串
    printf(" 比較結果爲:%d\n",x);
	//輸出字符數組1
    //puts(s1);
    //輸出字符數組2
    //puts(s2);

	system("pause");
	return 0;
}

七、strlwr函數—轉換爲小寫的函數

八、strupr函數—轉換爲大寫的函數

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//定義字符數組
	char s1[] = "HEI MA";
    char s2[] = "hei ma ";

    //複製字符串 
	strlwr (s1);
    strupr(s2);
    //輸出字符串
    //printf(" 比較結果爲:%d\n",s1);
	//輸出字符數組1
    puts(s1);
    //輸出字符數組2
    puts(s2);

	system("pause");
	return 0;
}








   




























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