Still continue to study and encourage yourself every day.
Character array
char word[] = {'H','e','l','l','o'};
This is not a string in c language. It can't be computed in the way of a string.
I. Strings
char word[] = {'H','e','l','l','o','\0'};
The difference is that there is an extra zero at the end, which becomes a string.
- A string of characters ending with 0 (integer 0)
- 0 or' 0'is the same, but different from'0'.
- 0 marks the end of the string, but it is not part of the string
- This 0 is not included in the calculation of string length
- Strings exist as arrays, are accessed as arrays or pointers, and more often as pointers.
- There are many functions in string.h that deal with strings.
String variables
char *str = "hello"; char word[] = "hello"; char line[10] = "hello";
string constant
- "hello"
- "hello" is put somewhere by the compiler into an array of characters, which is 6 in length and ends with 0 indicating the end.
- Two adjacent string constants are automatically connected
Character string
- c language strings exist in the form of character arrays
- Strings cannot be manipulated with operators
- Strings can be traversed through arrays
- The only special feature is that string literals can be used to initialize character arrays.
2. String Constants
char *s = "hello world";
- s is a pointer initialized to point to a string constant
- Because of the location of this constant, all s is actually const char* s, but for historical reasons, the compiler accepts writing without const
- But trying to write to the string referred to by s can lead to serious consequences.
- If you need to modify strings, you should use arrays
char s[] = "hello , world!";
Pointer or Array
char *str = "hello"; char word[] = "hello";
Array: Readable, Writable
Pointer: Read only, not modify
If you want to construct a string - --> array
If you want to process a string - --> pointer
char* is a string
- Strings can be expressed in the form of char*
- char* is not necessarily a string
- The original meaning is a pointer to a string, possibly an array of characters.
- Only if the end of the character array it refers to is zero can it be said that it refers to a string.
#include <stdio.h> int main(void) { char *s = "hello world"; char *s1 = "hello world"; char s3[] = "hello world"; // This can't be modified. // s[0] = 'B'; s3[0] = 'B'; printf("%p\n", s); printf("%p\n", s1); printf("%p\n", s3); printf("Here! s[0]=%c\n", s[0]); printf("Here! s3[0]=%c\n", s3[0]); // 0000000000404000 // 0000000000404000 // 000000000062FE30 // Here! s[0]=h // Here! s3[0]=B return 0; }
III. Input and Output of Strings
String assignment
char *t = 'title'; char *s; s = t;
There is no new string generated, just pointer s to the string referred to by T. Any operation on S is done on t.
String input and output
char string[8]; scanf("%s" , string); printf("%s" , string);
Scnf reads in a word (until space, tab, return)
Scnf is not safe because you don't know the length of the content to be read in
Common mistakes
- char *string;
- scanf("%s" , string);
- Considering that char* is a string type, a variable string with a string type is defined and can be used directly.
- Since there is no initialization of string 0, it is not always possible to make errors every time you run it
IV. Single character input and output
int putchar(int c);
- Write a character to standard output
- Return to write several characters, EOF(-1) indicates write failure
int getchar(void);
- Read a character from standard input
- The return type is int to return EOF (-1) windows - - > Ctrl-z
5. String function strlen
- size_t strlen(const char *s);
- Returns the string length of s (excluding 0 at the end)
#include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { char line[] = "hello"; printf("strlen=%lu\n", strlen(line)); printf("sizeof=%lu\n", sizeof(line)); return 0; } //strlen=5 // This includes the last 0 of the string. //sizeof=6
Of course, you can also write a function to calculate the length of the string.
#include <stdio.h> #include <string.h> // Custom Method for Calculating String Length int mylen(const char *s) { int idx = 0; while ( s[idx] != '\0' ){ idx++; } return idx; } int main(int argc, char const *argv[]) { char line[] = "hello"; printf("strlen=%lu\n", mylen(line)); printf("sizeof=%lu\n", sizeof(line)); return 0; }
6. String function strcmp
- int strcmp(const char s1 , const char s2);
- Comparing two strings returns:
- 0: s1 == s2
- 1: s1 > s2
- -1: s1 < s2
String function strcpy
- char* strcpy(char restrict dst , const char restrict src);
- Copy the string of src to dst
- restrict indicates that src and dst do not overlap (c99)
- Return to dst in order to connect the code
Copy a string
char *dst = (char*)malloc(Strlen(src)+1); strcpy(dst , src);
8. String strcat
- char* strcat(char restrict s1 , const char restrict s2);
- Copy s2 to the back of s1 and join it into a long string
- Return to s1
- s1 must have enough space
But these functions are unsafe, because you don't know if there is enough space, so
Secure Version: Figure
9. String search function
- char * strchr(const char *s , int c);
- char * strrchr(const chat *s int c);
- Return NULL to indicate that it was not found
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char const *argv[]) { char s[] = "hello"; char *p = strchr(s, 'l'); // Find the second l p = strchr(p+1,'l'); printf("%s\n", p); return 0; }
Copy to another
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char const *argv[]) { char s[] = "hello"; char *p = strchr(s, 'l'); char *t = (char*)malloc(strlen(p)+1); strcpy(t,p); printf("%s\n", t); free(t); // llo return 0; }