c/c++の文字列処理に関連する関数

29369 ワード

void *memccpy (void *dest, const void *src, int c, size_t n);
srcが指すオブジェクトからdestが指すオブジェクトにn文字をコピーします.コピー中に文字cに遭遇した場合、コピーを停止し、ポインタがdest中の文字cの次の位置を指す.そうでなければNULLを返します.
void *memcpy (void *dest, const void *src, size_t n);
srcが指すオブジェクトからdestが指すオブジェクトにn文字をコピーします.ポインタがdestの値を返します.
void *memchr (const void *s, int c, size_t n);
sが指すオブジェクトの前のn文字の中で文字cを検索します.検索された場合、戻りポインタは文字cが最初に現れた位置を指す.そうでなければNULLを返します.
int memcmp (const void *s1, const void *s2, size_t n);
s 1が指すオブジェクトとs 2が指すオブジェクトの最初のn文字を比較します.戻り値は、s 1とs 2の最初の異なる文字差です.
int memicmp (const void *s1, const void *s2, size_t n);
s 1が指すオブジェクトとs 2が指すオブジェクトの最初のn文字を比較し、大文字と小文字を無視します.戻り値は、s 1とs 2の最初の異なる文字差です.
void *memmove (void *dest, const void *src, size_t n);
srcが指すオブジェクトからdestが指すオブジェクトにn文字をコピーします.ポインタがdestの値を返します.メモリ重複は発生しません.
void *memset (void *s, int c, size_t n);
sが指すオブジェクトの最初のn文字を文字cに設定します.ポインタがsの値を返します.
char *stpcpy (char *dest, const char *src);
文字列srcをdestにコピーします.ポインタがdest+len(src)の値を返します.
char *strcpy (char *dest, const char *src);
文字列srcをdestにコピーします.ポインタがdestの値を返します.
char *strcat (char *dest, const char *src);
文字列srcをdestの末尾に追加します.ポインタがdestの値を返します.
char *strchr (const char *s, int c);
文字列sで文字cを検索します.検索された場合、戻りポインタは文字cが最初に現れた位置を指す.そうでなければNULLを返します.
int strcmp (const char *s1, const char *s2);
文字列s 1と文字列s 2を比較します.戻り値は、s 1とs 2の最初の異なる文字差です.
int stricmp (const char *s1, const char *s2);
文字列s 1と文字列s 2を比較し、大文字と小文字を無視します.戻り値は、s 1とs 2の最初の異なる文字差です.
size_t strcspn (const char *s1, const char *s2);
戻り値は、文字列s 1の文字列s 2に含まれない文字からなる初期列長である.
size_t strspn (const char *s1, const char *s2);
戻り値は、文字列s 1の文字列s 2に含まれる完全な文字からなる初期列長である.
char *strdup (const char *s);
文字列sのコピーを取得します.ポインタがコピーされた文字列の先頭アドレスを指します.
char *strerror(int errnum);
戻りポインタは、errnumに関連付けられたエラーメッセージ文字列のヘッダアドレスを指します.errnumのマクロ定義はerrno.h.
size_t strlen (const char *s);
戻り値は文字列sの長さです.終了記号'/0'は含まれません.
char *strlwr (char *s);
文字列sをすべて小文字に変換します.ポインタがsの値を返します.
char *strupr (char *s);
文字列sをすべて大文字に変換します.ポインタがsの値を返します.
char *strncat (char *dest, const char *src, size_t maxlen);
文字列srcをdestの末尾に追加し、maxlen文字まで追加します.ポインタがdestの値を返します.
int strncmp (const char *s1, const char *s2, size_t maxlen);
文字列s 1と文字列s 2を比較し、maxlen文字まで比較します.戻り値は、s 1とs 2の最初の異なる文字差です.
char *strncpy (char *dest, const char *src, size_t maxlen);
文字列srcをdestにコピーし、maxlen文字までコピーします.ポインタがdestの値を返します.
int strnicmp(const char *s1, const char *s2, size_t maxlen);
文字列s 1と文字列s 2を比較し、大文字と小文字を無視し、maxlen文字まで比較します.戻り値は、s 1とs 2の異なる文字差です.
char *strnset (char *s, int ch, size_t n);
設定文字列sの最初のn文字はすべて文字cである.ポインタがsの値を返します.
char *strset (char *s, int ch);
設定文字列sの文字はすべて文字cです.ポインタがsの値を返します.
char *strpbrk (const char *s1, const char *s2);
戻りポインタは、文字列s 1における文字列s 2の任意の文字が初めて現れる位置を指す.NULLが表示されない場合は戻ります.
char *strrchr (const char *s, int c);
文字列sで文字cを検索します.検索された場合、戻りポインタは文字cが最後に現れた位置を指す.そうでなければNULLを返します.
char *strrev (char *s);
文字列をすべて反転し、ポインタが反転した文字列を指します.
char *strstr (const char *s1, const char *s2);
文字列s 1で文字列s 2を検索します.検索された場合、戻りポインタは文字列s 2が初めて現れた位置を指す.そうでなければNULLを返します.
char *strtok (char *s1, const char *s2);
文字列s 2の文字を区切り文字として文字列s 1を分割する.ポインタが分割された文字列を指すことを返します.最初の呼び出し後、s 1の代わりにNULLLを使用する必要があります.
c中:
//メモリ割付関数
void*  malloc(size_t  size);
 1 #include <stdio.h>

 2 #include <stdlib.h>

 3 int main()

 4 {

 5     int i, n;

 6     char *buffer;

 7     

 8     printf("how long do you want the string? ");

 9     scanf("%d", &i);

10     

11     buffer = (char*)malloc(i+1);

12     if(buffer == NULL)  //       

13         exit(1); 

14          memset(buffer, 0, i+1);      

15      for(n=0; n<i; n++)

16      {

17         buffer[n] = rand()%26 + 'a';

18      }

19      buffer[i] = '\0';

20     

21     printf("Random string: %s
", buffer); 22 free(buffer); 23 return 0; 24 }

//メモリ充填関数:
void * memset(void *dest, int c, size_t count);
 1 #include  <stdio.h>

 2 #include  <string.h>

 3 int main()

 4 {

 5             char str[]  = "almost every progreammer should know memset";

 6             memset(str,  '-',  6);

 7             puts(str);

 8             

 9             return 0;

10 }            

11 //  : ------every progreammer should know memeset;                        

memcpy
プロトタイプ:void*memcpy(void*destination,const void*source,size_t num);     .//パラメータはポインタです.
 1 #include <stdio.h>

 2 #include <string.h>

 3 

 4 struct {

 5     char name[40];

 6     int age;

 7 }person, person_copy;

 8 

 9 int main()

10 {

11     char myname[] = "Pierre de Fermat ";

12     //using memcpy to copy string;

13     memcpy(person.name, myname, strlen(myname)+1 );

14     person.age = 46;

15 

16     //using memcpy to copy structure 

17     memcpy( &person_copy, &person, sizeof(person));

18     

19     printf("person_copy: %s, %d 
", person_copy.name, person_copy.age ); 20 21 return 0; 22 }
------ :
  person_copy: Pierre de Fermat , 46

 
 
 
//文字列内の文字関数の検索
char * strchr(const char* ,  int );
 1 #include <stdio.h>

 2 #include <string.h>

 3 

 4 int main()

 5 {

 6     char str[] = "This is a  sample ";

 7     //char ch = 's'; ----      

 8     int ch = 's';

 9     char *pch;

10     printf("looking for the 's' character in  \"%s\" ...
", str); 11 12 pch = strchr(str, ch); 13 while(pch != NULL) 14 { 15 printf("%s
", pch); 16 printf("found at %d
", pch-str+1);  // 17 pch = strchr(pch+1, 's'); 18 } 19 20 return 0; 21 }

looking for the 's' character in  "This is a  sample " ...
s is a  sample
found at 4
s a  sample
found at 7
sample
found at 12

//文字列分割関数
 char *strtok(char *str1, char *str2);
str 2の集合、str 1はここの各文字に基づいて分割されます
 1 //   strtok  ,         .       ,       ;

 2 #include <stdio.h>

 3 #include <string.h>

 4 

 5 int main()

 6 {

 7     char str[] = " this , a  sample, string";

 8     char *pch;

 9     char *psh = ",";

10     

11     printf("Splitting string \"%s\" into to 
", str); 12 13 pch = strtok(str, psh); // , ;----- ; 14 while(pch !=NULL) 15 { 16 printf(" :%s
", str); 17 printf(" :%s
", pch); 18 pch = strtok(NULL, psh); // 2, 3,。。。。 , NULL 19 } 20 21 return 0; 22 }

Splitting string " this , a  sample, string" into to
: this
: this
: this
: a  sample
: this
: string

strcpy   //strncpy()
プロトタイプ:char*strcpy(char*destination,const char*source);//パラメータはポインタです.
プロトタイプ:char*strncpy(char*destination,const char*source,size_t num);//パラメータはポインタです.
 1 #include <stdio.h>

 2 #include <string.h>

 3 

 4 int main()

 5 {

 6     char str1[] = "Sample string ";

 7     char str2[40];

 8     char str3[40];

 9 

10     strcpy(str2, str1);

11     strcpy(str3, "copy successful ");

12     

13     printf("str1: %s
str2: %s
str3: %s
", str1, str2, str3); 14 15 return 0; 16 }
-----
str1: Sample string
 str2: Sample string
 str3: copy successful

 
 
c++中
string::c_str()
1.stringクラスメンバー関数c_str()のプロトタイプ:
   const char *c_str() const;//nullで終了したc文字列を返します.
2.c_str()関数は、正規のc文字列を指すポインタを返し、内容はstringクラスの自体のオブジェクトと同じであり、stringクラスのc_を通過する.str()関数はstringオブジェクトをc中の文字列のスタイルに変換することができる.
これは、c言語と互換性があるため、c言語にはstringタイプがないため、stringクラスオブジェクトのメンバー関数c_を通過する必要があります.str()stringオブジェクトをcの文字列スタイルに変換します.
注意:strcpy()関数などを使用して方法c_を操作する必要があります.str()が返すポインタ
1 char * c;

2 string str = "12345";

3 c = str.c_str();     ////c          ,  s     ,       

4 

5 6 char c[20];

7 string s="1234";

8 strcpy(c,s.c_str()); 

9        ,c_str()          ,        
 1 #include <iostream>

 2 #include <cstring>

 3 #include <string>

 4 

 5 int main()

 6 {

 7     std::string str("please split this sentense into tokens");

 8     

 9     char *cstr = new char[str.length()+1];

10     std::strcpy(cstr, str.c_str());  //c_str()    ;

11 

12     std::cout <<"cstr = "<< cstr << std::endl;

13     char *p = std::strtok(cstr, " ");

14     while(p != 0)

15     {

16         std::cout << p << '
'; 17 p = strtok(NULL, " "); 18 } 19 20 delete[] cstr; 21 return 0; 22 }

string::length//string::size()に等しい
プロトタイプ:size_t length() const;
return length of string
 1 #include <iostream>

 2 #include <string>

 3 

 4 int main()

 5 {

 6     std::string str("test string ");

 7     std::cout << "the size of str is " << str.length() << "  characters. 
"; 8 std::cout << "the size of str is " << str.size() << " characters.
"; 9 return 0; 10 }

string::resize
プロトタイプ:void resize(size_t n);
      void resize (size_t   n,  char  c);
resize関数はstringのサイズを変更するために使用され、sizeがstringのsizeのサイズより小さい場合は、前のsize文字を切り取り、より大きい場合はcで補足します.
 1 #include <iostream>

 2 #include <string>

 3 

 4 int main()

 5 {

 6     std::string str("I like to code in  C");

 7     std::cout << str << std::endl;

 8     

 9     unsigned sz = str.size();

10     std::cout << sz << std::endl;

11     std::cout << str << std::endl;

12 

13     str.resize(sz+2, '+');

14     std::cout << str << std::endl;

15         

16     str.resize(14);

17     std::cout << str << std::endl;

18     str.resize(24);

19     std::cout << str << std::endl;

20     

21     return 0;

22 }
----- :
I like to code in  C
20
I like to code in  C
I like to code in  C++
I like to code in  C++
I like to code