C字符串處理函數

1、strcat

char * strcat ( char * destination, const char * source );
連接字符串

將source字符串的拷貝附加到destination字符串,destination中的結束空字符null被source字符串的第一個字符所覆蓋,並且一個新的空字符null被附加到連接兩個字符串而生成的新字符串destination的末尾。

參數

destination

       指向destination數組的指針,它包含一個C字符串,並且應該足夠大以便可以容納連接後的結果字符串。

source

       被附加到destination的C字符串。它不應該與destination重疊。

返回值

       返回destination。

Example:

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}

Output:

these strings are concatenated.

2、strchr

const char * strchr ( const char * str, int character );
      char * strchr (       char * str, int character );
定位一個字符在字符串中的第一次出現

返回一個指向在C字符串str中字符character第一次出現的位置的指針。

結束空字符null被算作是C字符串的一部分。因此,它也可以被定位,以便獲取一個指向字符串末尾的指針。

參數

str

       C字符串。

character

       需要被定位的字符。它被作爲整型值傳遞,但是他最終是被轉換回char類型。

返回值

返回一個指向在str中character第一次出現的位置的指針。

拂過沒有找到,則返回空指針null。

可移植性

在C語言中,這個函數被聲明爲:

char * strchr ( const char *, int ); 
而不是C++中提供的兩個衝在版本。

Example:

/* strchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}

Output:

Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18

3、strcmp

int strcmp ( const char * str1, const char * str2 );
比較兩個字符串

比較C字符串str1和C字符串str2.

這個函數從比較兩個字符串的第一個字符開始。如果這兩個字符相等,則比較餘下的字符對直到兩個字符不相等或到達結束空字符null。

參數

str1

       要比較的C字符串。

str2

      要比較的C字符串。

返回值

返回一個指示兩個字符串之間的關係的整型值:

0值指示兩個字符串是相等的。

大於0的值指示,對第一個不匹配的字符,str1的字符值大於str2的,反之則返回一個小於0的值。

Example:

/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szKey[] = "apple";
  char szInput[80];
  do {
     printf ("Guess my favourite fruit? ");
     gets (szInput);
  } while (strcmp (szKey,szInput) != 0);
  puts ("Correct answer!");
  return 0;
}

Output:

Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!

4、strcoll

int strcoll ( const char * str1, const char * str2 );
功能和strcmp類似,用法也一樣。

特別注意:

       strcoll()會依環境變量LC_COLLATE所指定的文字排列次序來比較s1和s2 字符串。

       strcmp是根據ASCII來比較2個串的。

說明:若LC_COLLATE爲"POSIX"或"C",則strcoll()與strcmp()作用完全相同。


5、strcpy

char * strcpy ( char * destination, const char * source );
拷貝字符串

將source指向的C字符串拷貝到destination指向的數組,包括結束空字符串null。

爲了避免溢出,destination指向的數組的大小應該足夠長以便能容納C字符串source(包括結束空字符串null),並且在內存中不能與source重疊。

參數

destination

       指向要複製內容的目的數組的指針。

source

       要複製的C字符串。

返回值

返回destination。

Example:

/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

Output:

str1: Sample string
str2: Sample string
str3: copy successful

6、strcspn

to be continued...










發佈了30 篇原創文章 · 獲贊 31 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章