String functions commonly used string functions using C ++ finishing

 

Common C ++ string functions use sort

 

strlen (array of characters)

  • Function: string length requirements.
  • Description: This argument function may be a character array name, or a string.
  • Sample use:
char s1[80] = "China";

cout<<strlen(s1)<<'\n';      //输出结果为5

cout<<strlen("大学生")<<'\n';    //输出结果为6 
  • Results Description: a character with two bytes, so the results strlen ( "Students") is 6.

strcpy (an array of characters, character array 2)

  • Function: Copy character array 2 to the character string in an array
  • Description:
    length (1) of a character array of characters must be greater than the length of the array 2.
    (2) when copying a string of replicated together with the rear '\ 0' to an array of characters.
    (3) can not use an assignment statement or a string constant array of characters assigned directly to an array of characters.
    Copy (4) an array of characters can only be treated with strcpy function. With an assignment Only one character is assigned to a character variable or character array element. But it can be initialized when defined.

Such as the following form:

str1 ={''Good"};    //不合法

str1 = str2;      //不合法

char a[5],c1,c2;

c1 = 'A'; c2 = 'B';    //合法

c[0] = 'C';       //合法

char g[20] = "aaaa''    //合法
  • Sample use:

`char a[20]="aaaaaa",b[20]="bbb";`
`strcpy(a,b);`
`cout<<a; `
`return 0;`
  • Results Description: The value of the array b will cover a array of values, so the result is "bbb".

strcat (an array of characters, character array 2)

  • Function: a character string array 2 is connected to the string in the character array 1, it has no effect on the content in the second character array.
  • Description: This function of the second parameter may be a string constant.
  • Sample use:

char s1[20] = "one", s2 = "two", s3[20] = "three";

strcat(s1,s2);

strcat(s1,s3);
  • Results Description: After the run the sample, the array of string s1 "onetwothree", s2 and s3 array of string has not changed.

strcmp (an array of characters, character array 2)

  • Function: Compares two strings are equal.
  • 说明:
    (1)如果两个字符串中的字符均相同,则两个字符串相等,函数返回值为0;
    (2)当两个字符串不同时,则以自左至右出现的第一个不同字符的比较结果作为两个字符串的比较结果。
       如果第一个字符串大于第二个字符串,则返回值为1。
       如果第一个字符串小于第二个字符串,则返回值为-1。
    (3)这种比较是按字符的ASCII码值的大小比较的。
  • 使用样例:

strcmp("Student","Student");      //比较结果为0

strcmp("student","Student");      //比较结果为1

strcmp("Student","student");      //比较结果为-1

int a=strcmp("stude","student");
  • 结果说明:当第一个字符串比较完后,第二个字符串还有字符,则当第一个字符串小于第二个字符串,所以a的值为-1。

strlwr(字符数组)

  • 功能:将字符数组中存放的所有大写字母变成小写字母,其它字母不变。
  • 使用样例:

char s1[ ] = "Student1";

strlwr (s1);
  • 结果说明:将s1数组中的字符串全部变成小写字母,即“student1"。

strupr(字符数组)

  • 功能:将字符数组中存放的所有小写字母变成大写字母,其它字母不变。
  • 使用样例:

char s1[ ] = "Student2";

strupr (s1);
  • 结果说明:将s1数组中的字符串全部变成小写字母,即“STUDENT2"。

strncpy(字符数组1,字符数组2,len)

  • 功能:将字符数组2 前len个字符复制到字符数组1的前len个字符空间中。
  • 说明:
    (1)第二个参数可以是数组名,也可以是字符串,第三个参数为正整数。
    (2)当字符数组2中表示的字符串的长度小于len时,则将该字符串全部复制到第一个参数所指定的数组中。
  • 使用样例:

char s1[ 80] = "aaaaaa", s2[80];

strncpy(s1,"student", 4);

strncpy(s2,"teacher",10);
  • 结果说明:
    运行该样例后,s1为"studaa";字符串"teacher"的长度小于10,则将其全部字符复制到s2中,s2的内容为"teacher"。

strncmp(字符数组1,字符数组2,len)

  • 功能:比较两个字符数组中表示的字符串的前len个字符。
  • 说明:
    (1)前两个参数均可以为字符数组或字符串,第3个参数为正整数。
    (2)若第一个字符串或第二个字符串的长度小于len时,该功能与strcmp()相同。
    (3)当两个字符串的长度均大于len时,len为最多要比较的字符个数。
  • 使用样例:
    cout<<strncmp("English","England",4)<<endl;
  • 结果说明:因为比较的两个字符串的前4个字符相同,所以输出的值为0。

strlen(字符数组)

  • 功能:求字符串长度。
  • 说明:该函数的实参可以是字符数组名,也可以是字符串。
  • 使用样例:
char s1[80] = "China";

cout<<strlen(s1)<<'\n';      //输出结果为5

cout<<strlen("大学生")<<'\n';    //输出结果为6 
  • 结果说明:一个汉字有两个字节,所以strlen("大学生")的结果为6。

strcpy(字符数组1,字符数组2)

  • 功能:将字符数组2中的字符串复制到字符数组1中
  • 说明:
    (1)字符数组1的长度必须大于等于字符数组2的长度。
    (2)复制时连同字符串后面的'\0'一起复制到字符数组1中。
    (3)不能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。
    (4)字符数组的复制只能用strcpy函数处理。用一个赋值语句只能将一个字符赋给一个字符型变量或字符型数组元素。但可以在定义的时候初始化。

如以下形式:

str1 ={''Good"};    //不合法

str1 = str2;      //不合法

char a[5],c1,c2;

c1 = 'A'; c2 = 'B';    //合法

c[0] = 'C';       //合法

char g[20] = "aaaa''    //合法
  • 使用样例:

`char a[20]="aaaaaa",b[20]="bbb";`
`strcpy(a,b);`
`cout<<a; `
`return 0;`
  • 结果说明:数组b的值将会覆盖数组a的值,所以结果为"bbb"。

strcat(字符数组1,字符数组2)

  • 功能:将字符数组2中的字符串连接到字符数组1中的字符串的后面,对字符数组2中的内容没有影响。
  • 说明:该函数中的第二个参数也可以是一个字符串常量。
  • 使用样例:

char s1[20] = "one", s2 = "two", s3[20] = "three";

strcat(s1,s2);

strcat(s1,s3);
  • 结果说明:运行样例后,则数组s1中的字符串为”onetwothree",数组s2和s3中的字符串没变。

strcmp(字符数组1,字符数组2)

  • 功能:比较两个字符串是否相等。
  • 说明:
    (1)如果两个字符串中的字符均相同,则两个字符串相等,函数返回值为0;
    (2)当两个字符串不同时,则以自左至右出现的第一个不同字符的比较结果作为两个字符串的比较结果。
       如果第一个字符串大于第二个字符串,则返回值为1。
       如果第一个字符串小于第二个字符串,则返回值为-1。
    (3)这种比较是按字符的ASCII码值的大小比较的。
  • 使用样例:

strcmp("Student","Student");      //比较结果为0

strcmp("student","Student");      //比较结果为1

strcmp("Student","student");      //比较结果为-1

int a=strcmp("stude","student");
  • 结果说明:当第一个字符串比较完后,第二个字符串还有字符,则当第一个字符串小于第二个字符串,所以a的值为-1。

strlwr(字符数组)

  • 功能:将字符数组中存放的所有大写字母变成小写字母,其它字母不变。
  • 使用样例:

char s1[ ] = "Student1";

strlwr (s1);
  • 结果说明:将s1数组中的字符串全部变成小写字母,即“student1"。

strupr(字符数组)

  • 功能:将字符数组中存放的所有小写字母变成大写字母,其它字母不变。
  • 使用样例:

char s1[ ] = "Student2";

strupr (s1);
  • 结果说明:将s1数组中的字符串全部变成小写字母,即“STUDENT2"。

strncpy(字符数组1,字符数组2,len)

  • 功能:将字符数组2 前len个字符复制到字符数组1的前len个字符空间中。
  • 说明:
    (1)第二个参数可以是数组名,也可以是字符串,第三个参数为正整数。
    (2)当字符数组2中表示的字符串的长度小于len时,则将该字符串全部复制到第一个参数所指定的数组中。
  • 使用样例:

char s1[ 80] = "aaaaaa", s2[80];

strncpy(s1,"student", 4);

strncpy(s2,"teacher",10);
  • 结果说明:
    运行该样例后,s1为"studaa";字符串"teacher"的长度小于10,则将其全部字符复制到s2中,s2的内容为"teacher"。

strncmp(字符数组1,字符数组2,len)

  • 功能:比较两个字符数组中表示的字符串的前len个字符。
  • 说明:
    (1)前两个参数均可以为字符数组或字符串,第3个参数为正整数。
    (2)若第一个字符串或第二个字符串的长度小于len时,该功能与strcmp()相同。
    (3)当两个字符串的长度均大于len时,len为最多要比较的字符个数。
  • 使用样例:
    cout<<strncmp("English","England",4)<<endl;
  • 结果说明:因为比较的两个字符串的前4个字符相同,所以输出的值为0。

Guess you like

Origin www.cnblogs.com/ziyuan122625/p/12000990.html