[String]

For example

char word[]={‘H’,‘e’,‘l’,‘l’,‘o’,"!",’\0’};

word[0] H
word[1] e
word[2] l
word[3] l
word[4] The
word[5] !
word[6] \0

definition

  • The end of the string is a string of characters 0 **
    0 or '\ 0' is the same, however, and '0' are different, '0' is a character, represented by decimal 48
  • 0 string end flag, he is not part of the string
    is not included when calculating the length of the string 0
  • In the form of an array of strings, accessing an array of pointers or
    more is in the form of a pointer
  • string.h there are a lot of string handling functions

A string of

char *str=“Hello”;
char word[]=“Hello”;
char line[10]=“Hello”;

String constant

  • "Hello", the compiler will be turned into a character array, the array length is 6, as well as indicating the end of the end 0
  • Two adjacent strings are connected automatically
  • C language string in the form of an array of characters
  • Do not operate on string operator
  • It can be traversed by means of a string array

String or an array of pointers?

= s * char "the Hello, World!";
s is a pointer, initialized to point to a string constant, due to the special position of the string is located, so that s is actually const char * s, so that the string pointed to by s can not be written, modified s [0], s [1 ] , etc.

If you need to modify the string, the array should be used:
char S [] = "! The Hello, World";

Array pointer
char s[]=“Hello”; char *s=“Hello”;
Here the string is automatically recovered as a local variable space This string does not know which processing parameter, dynamically allocated space
Constructs a string Processing a string

char * string is not necessarily

Char * string can be expressed as a form
but char * is not necessarily a string
intention is a pointer to the character, may point to an array of characters in
the character array has only referred to it at the end of 0, it refers to the characters string

String calculation

1. The input and output% s

 char string[8];
 scanf("%7s",string);
 printf("%7s",string);

scanf reads a word (read space, tab, carriage return date)
% 7S indicates that only read up to seven characters (as well as ending \ 0)
% S inside the numbers indicate the maximum number of characters allowed to read this digital array of numbers should be better than the small one

2. Remember pointer initialization
char * string to remember is initialized to 0, then scanf

String Functions

#include <string .h>

  • strlen s return string length (not including the terminating 0)
    input and output LU%
    Line char [] = "the Hello";
    the printf ( "% = strlen LU", strlen (Line));
  • strcmp (s1, s2) Compares two strings, returns
    0: s1 == s2
    positive difference: s1> s2
    negative difference: s1 <s2
  • strcpy copies the string src to dst
  • strcat s1 to s2 is followed by the copy to a long string into
Released nine original articles · won praise 0 · Views 90

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105293316