Introduction to C Language - String

Keywords: C Windows

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.

  1. A string of characters ending with 0 (integer 0)
  2. 0 or' 0'is the same, but different from'0'.
  3. 0 marks the end of the string, but it is not part of the string
  4. This 0 is not included in the calculation of string length
  5. Strings exist as arrays, are accessed as arrays or pointers, and more often as pointers.
  6. 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

  1. "hello"
  2. "hello" is put somewhere by the compiler into an array of characters, which is 6 in length and ends with 0 indicating the end.
  3. Two adjacent string constants are automatically connected

Character string

  1. c language strings exist in the form of character arrays
  2. Strings cannot be manipulated with operators
  3. Strings can be traversed through arrays
  4. The only special feature is that string literals can be used to initialize character arrays.

2. String Constants

char *s = "hello world";
  1. s is a pointer initialized to point to a string constant
  2. Because of the location of this constant, all s is actually const char* s, but for historical reasons, the compiler accepts writing without const
  3. But trying to write to the string referred to by s can lead to serious consequences.
  4. 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

  1. Strings can be expressed in the form of char*
  2. char* is not necessarily a string
  3. The original meaning is a pointer to a string, possibly an array of characters.
  4. 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

  1. char *string;
  2. scanf("%s" , string);
  3. Considering that char* is a string type, a variable string with a string type is defined and can be used directly.
  4. 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);
  1. Write a character to standard output
  2. Return to write several characters, EOF(-1) indicates write failure
int getchar(void);
  1. Read a character from standard input
  2. The return type is int to return EOF (-1) windows - - > Ctrl-z

5. String function strlen

  1. size_t strlen(const char *s);
  2. 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

  1. int strcmp(const char s1 , const char s2);
  2. Comparing two strings returns:
    • 0: s1 == s2
    • 1: s1 > s2
    • -1: s1 < s2

String function strcpy

  1. char* strcpy(char restrict dst , const char restrict src);
  2. Copy the string of src to dst
  3. restrict indicates that src and dst do not overlap (c99)
  4. 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

  1. char* strcat(char restrict s1 , const char restrict s2);
  2. Copy s2 to the back of s1 and join it into a long string
  3. Return to s1
  4. 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

  1. char * strchr(const char *s , int c);
  2. char * strrchr(const chat *s int c);
  3. 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;
}

Posted by superhaggis on Mon, 07 Oct 2019 00:59:07 -0700