当前位置:网站首页>Some notes on the use of C language strings

Some notes on the use of C language strings

2022-06-22 05:07:00 Jiangxueqian

String input problem

String input function :

  1. scanf(“%s”, str) Read in a string of characters , With Space 、 enter 、 Tab or EOF Is the separator symbol , Space 、 enter 、 Tab or EOF Will not be read in . The character after the separator will be treated as the next input .
  2. gets(str) Read in a line of characters , With Carriage return or EOF Is the separator symbol , Carriage return or EOF Will not be read in .

Out of commission Assignment statement Will a String constant or A character array Directly assigned to a A character array . The character array name is an address constant , It cannot change the value , Just as a numeric array name cannot be assigned . If the following two lines are illegal :

str1 = "China";
str2 = str1;

use Assignment statement Only one character Give to one Character variables or Character array elements . So a quick implementation is to use strcpy function Put a string Copy Go to another character array .

String copy function :strcpy(str1, str2) hold character string str2 Copy to character array str1 In the middle ( Include ’\0’). character string str2 The location of can also be a String constant . if str2 Valid characters digit <= str1, Will be partially or completely Cover . if str2 Number of valid characters > str1, May be an error .

String refers to the location copy function :strncpy(str1, str2, n) The string str2 in front n individual Copy characters to character array str1 In the middle , replace str1 Original front n individual character .


String length problem

When we initialize the character array , from brackets Whether to write or not Numbers There are two ways to write it :

  1. Numbers are written in brackets
    A. The number is equal to the actual size : System It will not be followed by '\0', The number of characters equals the size of the array equals the number .
     char str[6] = "abc123";
     char str[6] = {
          "abc123"};
     char str[6] = {
          'a', 'b', 'c', '1', '2', '3'};
    
    B. The number is not equal to the actual size : The system will be right Uninitialized element set '\0', The number of characters is different from that of numbers , The array size is equal to the number .
    char str[8] = "abc123";                       //"abc123\0\0"
    char str[8] = {
          "abc123"};                     //{"abc123\0\0"}
    char str[8] = {
          'a', 'b', 'c', '1', '2', '3'}; //{'a','b','c','1','2','3','\0','\0'}
    
  2. Brackets do not write numbers : In the first two cases, the system will Automatically add... At the end '\0', At this point, the size of the array is equal to the number of characters Add one . The third case is Different , The length of the array is equal to the number of characters .
    char str[] = "abc123";       //"abc123\0"
    char str[] = {
           "abc123" };   //{"abc123\0"}
    
    char str[] = {
           'a', 'b', 'c', '1', '2', '3' };
    // Therefore, when using this method to define , We usually add... Manually '\0', as follows :
    //char str[] = { 'a', 'b', 'c', '1', '2', '3', '\0' };
    

And we generally think of the string length , It refers to the number of valid characters in the character array , So... Is not included ‘\0’.
So using sizeof() Calculated Character array length Can not be used as String length Of , So we usually use strlen() function .strlen() function adopt '\0' To determine the end position of the string , And returns the string Number of valid characters , To calculate the String length .


Other string functions

String concatenation function :strcat(str1, str2)
Put the string str2 Splicing To string str1 Of Back ( Include '\0'), The result is put in str1 Character array , Therefore, we must ensure that str1 The array size meets the requirements . After the function call Return value yes str1 Character array Address .

String comparison function :strcmp(str1, str2)
be used for Compare str1 and str2, The comparison rule is to compare two strings From left to right Press... Character by character ASCII code Value size comparison , Until it appears Different characters Or encounter '\0' until . In fact, when using this function, we are more concerned about whether it equal Result , If All characters identical , Two strings are considered equal . Instead, I don't care who is bigger than who , String size doesn't make sense .

  1. If two strings are same , return 0.
  2. if str1>str2, Returns a positive integer .
  3. if str1<str2, Returns a negative integer .

String case conversion function

  1. strlwr(): In the string Capitalization The letters are converted to A lowercase letter Letter .
  2. strupr(): In the string A lowercase letter The letters are converted to Capitalization Letter .
原网站

版权声明
本文为[Jiangxueqian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220503534353.html