函數名: strcpy
功 能:
拷貝一個字元串到另一個
用 法: char *stpcpy(char *destin, char
*source);
程式例:
#include
<stdio.h>
<string.h>
int
main(void)
{
char
string[10];
char *str1 =
"abcdefghi";
stpcpy(string,
str1);
printf("%sn",
string);
return
0;
}
函數名:
strcat
字元串拼接函數
用 法: char *strcat(char *destin, char
destination[25];
char *blank = " ", *c =
"c++", *borland = "borland";
strcpy(destination, borland);
strcat(destination, blank);
strcat(destination, c);
destination);
strchr
在一個串中查找給定字元的第一個比對之處
用 法: char *strchr(char *str,
char c);
char
string[15];
char *ptr, c =
‘r‘;
strcpy(string, "this is a
string");
ptr = strchr(string,
c);
if
(ptr)
printf("the
character %c is at position: %dn", c,
ptr-string);
else
character was not foundn");
return
strcmp
功 能: 串比較
用 法: int
strcmp(char *str1, char *str2);
看asic碼,str1>str2,傳回值
> 0;兩串相等,傳回0
*buf1 = "aaa", *buf2 = "bbb", *buf3 =
"ccc";
int
ptr;
ptr = strcmp(buf2,
buf1);
if (ptr >
0)
printf("buffer 2
is greater than buffer 1n");
is less than buffer 1n");
ptr =
strcmp(buf2, buf3);
is greater than buffer 3n");
is less than buffer 3n");
strncmpi
功 能: 将一個串中的一部分與另一個串比較,
不管大小寫
用 法: int strncmpi(char *str1, char *str2,
unsigned maxlen);
char *buf1
= "bbb", *buf2 = "bbb";
int
ptr = strcmpi(buf2,
if (ptr >
printf("buffer 2 is
greater than buffer 1n");
if (ptr <
printf("buffer 2 is less
than buffer 1n");
if (ptr ==
printf("buffer 2 equals
buffer 1n");
strcpy
功 能: 串拷貝
用 法:
char *strcpy(char *str1, char
*str2);
char *str1 =
strcpy(string,
printf("%sn",
strcspn
在串中查找第一個給定字元集内容的段
用 法: int strcspn(char *str1, char
<alloc.h>
*string1 = "1234567890";
char *string2
= "747dc8";
length;
length = strcspn(string1,
string2);
printf("character where
strings intersect is at position %dn",
length);
strdup
将串拷貝到建立的位置處
用 法: char *strdup(char
*str);
*dup_str, *string = "abcde";
dup_str =
strdup(string);
dup_str);
free(dup_str);
函數名: strcmp
比較字元串str1和str2。
用 法: int strcmp(char *str1, char
說 明: 當s1<s2時,傳回值<0
當s1=s2時,傳回值=0
當s1>s2時,傳回值>0
即:兩個字元串自左向右逐個字元相比(按ascii值大小相比較),直到出現不同的字元或遇‘\0‘為止。
#include<stdio.h>
#include<string.h>
void main()
{
char string[20];
char str[3][20];
int i;
for(i=0;i<3;i++)
gets(str[i]);
if(strcmp(str[0],str[1])>0)
strcpy(string,str[0]);
else
strcpy(string,str[1]);
if(strcmp(str[2],string)>0)
strcpy(string,str[2]);
printf("\nthe
largest string is %s\n",string);
stricmp
以大小寫不敏感方式比較兩個串
用 法: int stricmp(char *str1, char
ptr = stricmp(buf2,
strerror
傳回指向錯誤資訊字元串的指針
用 法: char *strerror(int
errnum);
<errno.h>
*buffer;
buffer =
strerror(errno);
printf("error: %sn",
buffer);
strcmpi
功 能: 将一個串與另一個比較,
用 法: int strcmpi(char *str1, char
strncmp
int strncmp(char *str1, char *str2, int
maxlen);
int
= "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;
ptr =
strncmp(buf2,buf1,3);
less than buffer 1n");
strncmp(buf2,buf3,3);
greater than buffer 3n");
less than buffer 3n");
return(0);
功 能: 把串中的一部分與另一串中的一部分比較,
用 法: int strncmpi(char *str1, char
= "bbbccc", *buf2 = "bbbccc";
strncmpi(buf2,buf1,3);
strncpy
char *strncpy(char *destin, char *source, int
strncpy(string, str1,
3);
string[3] =
‘‘;
strnicmp
不注重大小寫地比較兩個串
用 法: int strnicmp(char *str1, char
*str2, unsigned
ptr = strnicmp(buf2, buf1,
strnset
将一個串中的所有字元都設為指定字元
用 法: char *strnset(char *str, char
ch, unsigned n);
*string = "abcdefghijklmnopqrstuvwxyz";
letter = ‘x‘;
printf("string before strnset:
%sn", string);
strnset(string, letter,
13);
printf("string after strnset:
strpbrk
在串中查找給定字元集中的字元
用 法: char *strpbrk(char *str1, char
*string1 = "abcdefghijklmnopqrstuvwxyz";
*string2 = "onm";
*ptr;
ptr = strpbrk(string1,
if
printf("strpbrk found
first character: %cn", *ptr);
printf("strpbrk didn‘t
find character in setn");
strrchr
在串中查找指定字元的最後一個出現
用 法: char *strrchr(char *str, char
char *ptr, c =
strcpy(string, "this is a
ptr = strrchr(string,
printf("the character
%c is at position: %dn", c, ptr-string);
was not foundn");
strrev
功 能: 串倒轉
char *strrev(char
*forward = "string";
printf("before strrev():
%sn", forward);
strrev(forward);
printf("after
strrev(): %sn", forward);
strset
用 法: char *strset(char *str, char
string[10] = "123456789";
char symbol =
‘c‘;
printf("before strset(): %sn",
strset(string,
symbol);
printf("after strset(): %sn",
strspn
在串中查找指定字元集的子集的第一次出現
用 法: int strspn(char *str1, char
char *string2 =
"123dc8";
length = strspn(string1,
printf("character where strings
differ is at position %dn", length);
strstr
在串中查找指定字元串的第一次出現
用 法: char *strstr(char *str1, char
char *str1
= "borland international", *str2 = "nation",
ptr = strstr(str1,
str2);
printf("the substring is: %sn",
ptr);
strtod
将字元串轉換為double型值
用 法: double strtod(char *str, char
**endptr);
<stdlib.h>
input[80], *endptr;
double
value;
printf("enter a floating point
number:");
gets(input);
value = strtod(input,
&endptr);
printf("the string is %s the
number is %lfn", input, value);
strsep
分解字元串為一組字元串。從str1指向的位置起向後掃描,遇到delim指向位置的字元後,将此字元替換為null,傳回str1指向的位址。
用
法: char *strtok(char **str1, const char
*delim);
int main()
{
int len, nel;
char
query[] ="user_command=appleboy&test=1&test2=2";
*q, *name, *value; /* parse into individualassignments */
q
= query; fprintf(stderr, "cgi[query string] : %s\n",query);
len = strlen(query);
nel = 1;
while (strsep(&q, "&"))
nel++;
fprintf(stderr, "cgi[nel string] : %d\n", nel);
for (q = query; q< (query +
len);)
{
value = name = q; /* skip to next
assignment */
fprintf(stderr, "cgi[string] :%s\n",
q);
fprintf(stderr, "cgi[stringlen] : %d\n",
strlen(q));
fprintf(stderr, "cgi[address] :%x\n",
for (q += strlen(q); q < (query +len) &&
!*q; q++); /* assign variable */
name =
strsep(&value,"=");
fprintf(stderr, "cgi[name ]
:%s\n", name);
fprintf(stderr, "cgi[value] :%s\n",
value);
}
return 0;
strtok
查找由在第二個串中指定的分界符分隔開的單詞
用 法: char *strtok(char *str1,
char *str2);
input[16] = "abc,d";
*p;
/* strtok places a null
terminator
in front of the token, if found
*/
p = strtok(input,
",");
if (p) printf("%sn",
p);
/* a second call to strtok using a
null
as the first parameter returns a
pointer
to the character following the
token */
p = strtok(null,
strtol
功 能: 将串轉換為長整數
法: long strtol(char *str, char **endptr, int
base);
*string = "87654321", *endptr;
long
lnumber;
/* strtol converts string to long
integer */
lnumber = strtol(string,
&endptr, 10);
printf("string = %s
long = %ldn", string, lnumber);
strupr
将串中的小寫字母轉換為大寫字母
用 法: char *strupr(char
*string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters
strupr(string);
swab
功 能: 交換位元組
法: void swab (char *from, char *to, int
nbytes);
char source[15] = "rfna koblrna
d";
char target[15];
swab(source, target, strlen(source));
printf("this is target: %sn", target);
}
ps:isalpha()是字元函數,不是字元串函數,
isalpha
原型:extern int isalpha(int
c);
用法:#include
<ctype.h>
功能:判斷字元c是否為英文字母
說明:當c為英文字母a-z或a-z時,傳回非零值,否則傳回零。
舉例:
//
isalpha.c
#include <syslib.h>
#include <ctype.h>
#include
<stdio.h>
main()
{
int
c;
clrscr(); // clear
screen
printf("press
a key");
for(;;)
c=getchar();
clrscr();
printf("%c: %s
letter",c,isalpha(c)?"is":"not");
return 0; // just
to avoid warnings by compiler