C語言學習筆記之 字元操作函式
阿新 • • 發佈:2020-09-19
字元操作函式
標頭檔案
#include <string.h>
strcpy字元拷貝
#include <string.h>
char * strcpy(char * dest,const char * source);
功能:將source所指的字串拷貝到dest所指的記憶體空間中,
'\0'也會被拷貝
引數:
dest:目的字串首地址
source:源字元首地址
返回:
成功:dest字串首地址
失敗:NULL
注意!如果dest所指的記憶體不夠大,則可能造成緩衝溢位
例:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char s[20] = ""; strcpy(s,"Hello\0world!"); puts(s); return 0; }
Hello
strncpy只拷貝前n個字元
#include <string.h>
char * strncpy(char * dest,const char * source,size_t n);
功能:將source所指字串的前n個字元拷貝到dest所指的記憶體空間中,
'\0'也會被拷貝
引數:
dest:目的字串首地址
source:源字元首地址
n:拷貝字元個數
返回:
成功:dest字串首地址
失敗:NULL
是否結束拷貝是由'\0'決定的
例:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char s1[20] = ""; char s2[20] = ""; strncpy(s1,"Hello\0world!",7); puts(s1); strncpy(s2,"Hello world!",7); puts(s2); return 0; }
Hello
Hello w
strcat字串拼接
#include <string.h>
char * strcat(char * dest,const char * source);
功能:將source所指的字串連線到dest字串後面,'\0'也會追加
引數:
dest:目的字串首地址
source:源字元首地址
返回:
成功:dest字串首地址
失敗:NULL
例:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char s1[20] = "Hello"; char s2[20] = " world"; strcat(s1,s2); puts(s1); return 0; }
Hello world
strncat
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s1[20] = "Hello";
char s2[20] = " world";
strncat(s1,s2,3);
puts(s1);
return 0;
}
Hello wo
strcmp比較字串大小
#include <string.h>
char * strcmp(const char *str1,const char *str2);
功能:比較str1和str2的ASCII字元大小
引數:
str1:字串1首地址
str2:字串2首地址
返回:
相等:0
大於:>0
小於:<0
不同作業系統會有差異
例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s1[20] = "Hello";
char s2[20] = "world";
printf("%d\n%d\n",strcmp(s1,"Hello"),strcmp(s1,s2));
return 0;
}
0
-1
strncmp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s1[20] = "Hello";
char s2[20] = "world";
printf("%d\n%d\n",strcmp(s1,"Hello"),strncmp(s1,s2,1));
return 0;
}
0
-47
組包解包
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s1[20];
// 將字串組包(格式化)後儲存到s1中
sprintf(s1,"Hello%d,%s",123,"abcd");
int a;
char b[5];
// 將字串解包後分別儲存到變數中
sscanf(s1,"Hello%d,%s",&a,&b);
printf("%d\n%s\n",a,b);
return 0;
}
123
abcd
strchr字元查詢
#include <string.h>
char * strchr(const char *str,int val);
功能:在str中尋找val第一次出現的位置
引數:
str:被查詢的字串首地址
val:匹配字元
返回:
成功:val第一次出現的地址
失敗:NULL
例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
printf("請輸入一個字串:");
char s[32];
scanf("%s",s);
getchar();
printf("請輸入要查詢的字元:");
char val;
val = getchar();
getchar();
char *ret = NULL;
ret = strchr(s,val);
if (ret == NULL)
{
printf("未找到%c\n",val);
}
else
{
printf("地址:%p\n位置:%d\n",ret,(ret-s));
}
return 0;
}
PS D:\> ./main
請輸入一個字串:abcdf
請輸入要查詢的字元:f
地址:0060FE7C
位置:4
PS D:\> ./main
請輸入一個字串:ac
請輸入要查詢的字元:d
未找到d
例2:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s[32] = "aebecede";
char *buf = &s[0];
char *ret = NULL;
while (ret = strchr(buf,'e'))
{
printf("地址:%#d\n",ret);
*ret = '@';
buf = ret + 1;
}
printf("%s\n",s);
return 0;
}
地址:6356601
地址:6356603
地址:6356605
地址:6356607
a@b@c@d@
strstr字串查詢
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
printf("請輸入一個字串:");
char s[32];
scanf("%s",s);
getchar();
printf("請輸入要查詢的字串:");
char val[32];
scanf("%s",val);
getchar();
char *ret = NULL;
ret = strstr(s,val);
if (ret == NULL)
{
printf("未找到%c\n",val);
}
else
{
printf("地址:%p\n位置:%d\n",ret,(ret-s));
}
return 0;
}
請輸入一個字串:abHelloc
請輸入要查詢的字串:Hello
地址:0060FE7E
位置:2