Zi 字媒體
2017-07-25T20:27:27+00:00
一把梭系列 ~ C語言範例 (0013) [字串 輸入/輸出、長度計算、複製、串接]
資料來源: https://openhome.cc/Gossip/CGossip/StringLengthCopyCat.html
https://www.cplusplus.com/reference/cwchar/wcslen/
https://www.cplusplus.com/reference/cwchar/wcscpy/
https://www.cplusplus.com/reference/cwchar/wcsncpy/
https://www.cplusplus.com/reference/cwchar/wcscat/
https://www.cplusplus.com/reference/cwchar/wcsncat/
https://www.cplusplus.com/reference/cwchar/wprintf/
https://www.cplusplus.com/reference/cwchar/wscanf/
https://www.cplusplus.com/reference/cstdio/puts/
https://docs.microsoft.com/zh-tw/cpp/c-runtime-library/reference/puts-putws?view=msvc-160
https://www.ibm.com/docs/en/i/7.1?topic=functions-fputws-write-wide-character-string
★前言:
★主題:
在〈字元陣列與字串〉說過,在 C 語言中,字串是一個字元陣列,最後一個字元以空字元 ‘\0’ 作結尾。
但由於它就具有各元素依序組合後的實際單詞義意,並且也依照不同編碼格式而有所不同,因此C語言有提供一系列的對應函數方便使用者使用。
下面我們就將C語言提供的相關功能進行分門別類的初步整理與介紹:
01.字串輸入函數:
int fputs(const char *str,FILE *stream);
int fputws(const wchar_t *wcs, FILE *stream);
02.字串輸出函數:
int puts(const char *str);
int _putws(const wchar_t *str);
03.字串長度計算函數:
size_t strlen(const char *str );
size_t wcslen (const wchar_t* wcs);
04.字串拷貝函數:
char *strcpy( char *restrict dest, const char *restrict src );
char *strncpy( char *restrict dest, const char *restrict src, size_t count );
wchar_t* wcscpy (wchar_t* destination, const wchar_t* source);
wchar_t* wcsncpy (wchar_t* destination, const wchar_t* source, size_t num);
05.字串串接函數:
char *strcat( char *restrict dest, const char *restrict src );
char *strncat( char *restrict dest, const char *restrict src, size_t count );
wchar_t* wcscat (wchar_t* destination, const wchar_t* source);
wchar_t* wcsncat (wchar_t* destination, const wchar_t* source, size_t num);
★code:
#include
#include
#include
#include
#include
#define LEN 80
int main()
{
/*
//預設編碼環境設定:
//Setting -> Editor ->Global compiler settings->Other options -> default
//Build options->compiler settings->Other options []
char buf[LEN];
puts("請輸入名稱...");
// fgets 會包括 enter 字元
fgets(buf, LEN, stdin);
int lenOfName1 = strlen(buf) + 1;
char name1[lenOfName1];
strcpy(name1, buf);
printf("名稱:%s", name1);
// 不複製換行字元
int lenOfName2 = lenOfName1 - 1;
char name2[lenOfName2];
// 設定全部字元為空字元
memset(name2, '\0', lenOfName2);
strncpy(name2, buf, lenOfName2);
printf("名稱:%s", name2);
char str1[] = "xyz";
char str2[] = "abc";
int len = strlen(str1) + strlen(str2) + 1;
char concated[len];
memset(concated, '\0', len);
strcat(concated, str1);
strcat(concated, str2);
printf("串接後:%s\n", concated);
//*/
//*
//使用寬字串/UTF-8 環境設定:
//Setting -> Editor ->Global compiler settings->Other options -> UTF-8
//Build options->compiler settings->Other options [-finput-charset=UTF-8]
setlocale(LC_ALL, "");
wchar_t buf[LEN];
wprintf(L"請輸入名稱...\n");
wscanf (L"%ls",buf);
int lenOfName1 = wcslen(buf);
wchar_t name1[lenOfName1];
wcscpy(name1, buf);
wprintf(L"名稱:%ls\n", name1);
// 不複製換行字元
int lenOfName2 = lenOfName1;
wchar_t name2[lenOfName2];
// 設定全部字元為空字元
memset(name2, '\0', lenOfName2);
wcsncpy(name2, buf, lenOfName2+1);
wprintf(L"名稱:%ls\n", name2);
wchar_t *str1 = L"xyz";//error: invalid initializer ~ str1[]= "xyz";
wchar_t *str2 = L"abc";
int len = wcslen(str1) + wcslen(str2);
wchar_t concated[len];
memset(concated, '\0', len);
wcscat(concated, str1);
wcscat(concated, str2);
wprintf(L"串接後:%ls\n", concated);
//*/
return 0;
}
★結果:
★延伸說明/重點回顧:
01.C語言的字串就是字元陣列
02.從函數介紹可以大致發現寬字串和標準字串函數用法觀念幾乎一致,其差別就只有函數名稱和參數型態
03.如果當字串內會有非英數字的內容,建議將編譯器設定為UTF-8編譯模式,並且使用wchar_t資料型態
寫了
5860316篇文章,獲得
23313次喜歡