1.strncmp ()函數
strncmp ()函數可以比較到字符串的不同處,也可以比較完由第三個參數指定的字符串。
例如,如果想搜索以”astro”開頭的字符串,您可以限定搜索前5個字符。
//使用strncmp ()函數
#include <stdio.h>
#inlcude <string.h>
#define LISTSIZE 5
int main (void)
{
char * list[LISTSIZE] = {
"astronomy", "astounding",
"astrophysics", "ostracize",
"asterism"
};
int count = 0;
int i;
for (i=0; i < LISTSIZE; i++)
{
if (strncmp (list[i], "astro", 5) == 0)
{
printf ("Found: %s\n", list[i]);
count++;
}
}
printf ("The list contained %d words beginning with astro.\n", count);
return 0;
}
2.strcpy () 和strncpy ()函數
如果pts1和pts2都是指向字符串的指針,則下面的表達式只復制字符串的地址而不是字符串本身。
如果你確實希望復制字符串,那么可以使用strcpy ()函數。
//copy1.c strcpy ()示例
#include <stdio.h>
#include <string.h> //聲明strcpy ()函數
#define SIZE 40
#define LIM 5
int main (void)
{
char qwords[LIM][SIZE];
char temp[SIZE];
int i = 0;
printf ("Enter %d words beginning with q:\n", LIM);
while (i < LIM && gets(temp))
{
if (temp[0] != 'q')
{
printf ("%s doesn't begin with q!\n", temp);
}
else
{
strcpy (qwords[i], temp);
i++;
}
}
puts ("Here are the words accepted:");
for (i=0; i < LIM; i++)
{
puts (qwords[i]);
}
return 0;
}
請注意只有當輸入的單詞通過了q判斷,計數值i才會增加。
這里使用了一個基于字符的判斷
這相當于
if (strncmp (temp, "q", 1) != 0)
注意在,strcpy 兩個參數中,第二個參數temp指向的字符串被復制到第一個參數qword[i]所指向的數組中。
temp被稱為源字符串,qword被稱為目標字符串。
為什么要這么設計,為什么不設計到前面?
如果注意到它和賦值語句的順序一樣,目標在左邊,就很容易記住參數的順序。
3.strcpy ()的其它特性
首先,它是一個char *類型,它返回的是第一個參數的值。
第二,第一個參數不需要指向數組的開始,這樣就可以只復制數組的一部分。
//copy2.c
#include <stdio.h>
#include <string.h>
#define WORDS "beast"
#define SIZE 40
int main (void)
{
char *orig = WORDS;
char copy[SIZE] = "Be the best that you can be.";
char *ps;
puts (orig);
puts (copy);
ps = strcpy (copy + 7, orig);
puts (copy);
puts (ps);
return 0;
}
注意ps指向copy的第8個元素,這是因為第一個參數是copy + 7。
4. strncpy ()
strcpy ()的缺點顯而易見,它不檢查目標字符串是否能容納得下源字符串。
strncpy ()會用第三個參數指明最大可復制的字符數。
//copy3.c strncpy ()示例
#include <stdio.h>
#include <string.h> //聲明strcpy ()函數
#define SIZE 40
#define TARGSIZE 7
#define LIM 5
int main (void)
{
char qwords[LIM][TARGSIZE];
char temp[SIZE];
int i = 0;
printf ("Enter %d words beginning with q:\n", LIM);
while (i < LIM && gets(temp))
{
if (temp[0] != 'q')
{
printf ("%s doesn't begin with q!\n", temp);
}
else
{
strncpy (qwords[i], temp, TARGSIZE - 1);
qwords[i][TARGSIZE-1] = '\0';
i++;
}
}
puts ("Here are the words accepted:");
for (i=0; i < LIM; i++)
{
puts (qwords[i]);
}
return 0;
}
注意,在這里函數復制的字符串可能最后沒有空字符,所以你必須手動在最后放置一個空字符,而且注意這里是復制n個字符過去,所以要留一個給空字符
,所以在這里是TARGSIZE - 1;
strncpy (qwords[i], temp, TARGSIZE - 1);
qwords[i][TARGSIZE-1] = '\0';
5.sprintf ()函數
sprintf ()函數是在stdio.h而不是在string.h中聲明的。
它的作用和printf ()一樣,但是它寫到字符串里而不是寫到輸出顯示。
它提供了把幾個元素組合成一個字符串的一種途徑。
sprintf ()的第一個參數是目標字符串地址。
//format.c
#include <stdio.h>
#define MAX 20
int main (void)
{
char first[MAX];
char last[MAX];
char formal[2 * MAX + 10];
double prize;
puts ("Enter your first name: ");
gets (first);
puts ("Enter your last name: ");
gets (last);
puts ("Enter your prize money: ");
scanf ("%1f", prize);
sprintf (formal, "%s, %-19s: $%6.2f\n", last, first, prize);
puts (formal);
return 0;
}
6.其它字符串函數
char *strcpy (char * s1, const char * s2);
該函數把s2指向的字符串(包括空字符)復制到s1指向的位置,返回值是s1。
char *strncpy (char * s1, const char *s2, size_t n);
該函數把s2指向的字符串(包括空字符)復制到s1指向的位置,復制的字符數不超過n個。返回值是s1。
空字符后的字符串不被復制。
如果源字符串的字符數小于n個,在目標字符串中就以空字符填充。
如果源字符串的字符數大于或等于n個,空字符就不被復制。返回值是s1。
char *strcat (char *s1, const char *s2);
s2指向的字符串(包括空字符)復制到s1指向的結尾。復制過來的s2覆蓋了s1的空字符。返回值為s1。
char *strncat (char *s1, const char *s2);
s2指向的字符串中的前n個字符(包括空字符)復制到s1指向的結尾。復制過來的s2覆蓋了s1的空字符。
s2字符串中的空字符及其后的任何字符都不會被復制,并且追加一個空字符到所得結果后面。
返回值為s1。
int strcmp (const char *s1, const char *s2);
如果s1字符串在機器編碼順序中落后于s2字符串,函數的返回值是一個正數;如果兩個字符串相同,返回值是0;
如果第一個字符串在機器編碼順序中先于第二個字符串,返回值是一個負數。
int strncmp (const char *s1, const char *s2);
該函數作用和strcmp ()一樣,只是比較n個字符后或者遇見第一個空字符時會停止比較。
char *strchr (const char *s1, int c);
該函數返回一個指向字符串s中存放字符c的第一個位置的指針(標志結束的空字符是字符串的一部分,因此也可以搜索到它)。
如果沒有找到該字符,函數就返回空指針。
char *strpbrk (const char *s1, const char *s2);
該函數返回一個指針,指向字符串s1中存放s2字符串的任何字符的第一個位置。如果沒有找到該字符。如果沒找到任何字符,返回空指針。
char *strrchr (const char * s, int c);
該函數返回一個指針,指向字符串s中字符c最后一次出現的地方。(標志結束的空字符是字符串的一部分,因此也可以搜索到它)。
如果沒有找到該字符,函數就返回空指針。
char *strstr (const char *s1, const char *s2)
該函數返回一個指針,指向s1字符串中第一次出現s2字符串的地方。如果在s1中沒找到s2字符串,函數就返回空指針。
size_t strlen (const char *s);
返回字符串的長度。
簡單應用:
在用fgets ()函數時,讀取一行輸入,這個函數把換行符存儲到目標字符串中。
我們可以使用strchr ()函數來用一個空字符替換這個換行符。
char line [80];
char * find;
fgets (line, 80, stdin);
find = strchr (line, '\n');
if (find)
{
*find = '\0';
}