Comprehensive Summary of C Language String Handling Functions with Specific Usage Examples

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute every day to remember the basics of C language.

C Language Beginner’s Must-Learn Knowledge Points Note Series 100 Articles

The following notes finally enter the practical series, which is also the most important and difficult part of the C language. Keep it up! Youngsters!

35. Comprehensive Summary of C Language String Handling Functions with Specific Usage Examples

1. String Length Function: strlen

1. Function Prototype

size_t strlen(const char *str);

2. Function Characteristics

  • • Calculates the actual length of the string (excluding ‘\0’)
  • • Stops counting when ‘\0’ is encountered
  • • Time complexity O(n)

3. Usage Example

char str[] = "Hello";
printf("%zu", strlen(str));  // Outputs 5

4. Notes

char s[10] = {'a','b','c'}; 
printf("%zu", strlen(s));  // Result is uncertain because the string in the array is not terminated, missing '\0'

2. String Copy Function

1. strcpy Function

char* strcpy(char *dest, const char *src);
  • • Copies src to dest (including ‘\0’)
  • • Does not check the size of the destination buffer dest

2. strncpy Function (Safe Version, Specify Copy Size Limit)

char* strncpy(char *dest, const char *src, size_t n);
  • • Copies at most n characters
  • • Does not automatically add the terminator ‘\0’, must be handled manually

3. Usage Comparison

char dest1[10];
strcpy(dest1, "Hello");  // Unsafe

char dest2[10];
strncpy(dest2, "Hello", sizeof(dest2));
dest2[sizeof(dest2)-1] = '\0';  // Ensure termination

3. String Concatenation Function

1. strcat Function

char* strcat(char *dest, const char *src);
  • • Appends src to the end of dest
  • • Requires dest to have enough space

2. strncat Function (Safe Version)

char* strncat(char *dest, const char *src, size_t n);
  • • Appends at most n characters
  • • Automatically adds ‘\0’

3. Usage Example

char str[20] = "Hello";
strcat(str, " World");  // str becomes "Hello World"

char safe_str[20] = "Hello";
strncat(safe_str, " World!", sizeof(safe_str)-strlen(safe_str)-1);

4. String Comparison Function

1. strcmp Function

int strcmp(const char *s1, const char *s2);
  • • Return value:
    • • 0: strings are equal

    • 0: s1 > s2

    • • <0: s1 < s2

2. strncmp Function (Safe Version)

int strncmp(const char *s1, const char *s2, size_t n);
  • • Compares only the first n characters

3. Comparison Rules

  • • Compares in dictionary order
  • • Case-sensitive
if (strcmp("apple", "banana") < 0) {
    printf("apple comes first\n");
}

5. String Search Functions

1. strchr Function

char* strchr(const char *str, int c);
  • • Finds the first occurrence of character c
  • • Returns pointer or NULL

2. strrchr Function

char* strrchr(const char *str, int c);
  • • Finds the last occurrence of character c

3. strstr Function

char* strstr(const char *haystack, const char *needle);
  • • Finds the first occurrence of substring needle

4. Usage Example

char *p = strchr("Hello", 'l');
printf("%s\n", p);  // Outputs "llo"

p = strstr("Hello world", "wor");
printf("%s\n", p);  // Outputs "world"

6. String Tokenization Function: strtok

1. Function Prototype

char* strtok(char *str, const char *delim);

2. Usage Characteristics

  • • First call passes the string to be tokenized
  • • Subsequent calls pass NULL
  • • Modifies the original string (replaces delimiters with ‘\0’)

3. Usage Example

char str[] = "apple,orange,banana";
char *token = strtok(str, ",");
while (token) {
    printf("%s\n", token);
    token = strtok(NULL, ",");
}

7. Memory Operation Functions

1. memcpy Function

void* memcpy(void *dest, const void *src, size_t n);
  • • Copies n bytes (does not handle ‘\0’)
  • • Memory areas must not overlap

2. memmove Function

void* memmove(void *dest, const void *src, size_t n);
  • • Safely handles overlapping memory situations

3. memset Function

void* memset(void *str, int c, size_t n);
  • • Sets the first n bytes to c

8. Common Errors

1. Buffer Overflow

char buf[5];
strcpy(buf, "Hello");  // Overflow!

Correction:

strncpy(buf, "Hello", sizeof(buf));
buf[sizeof(buf)-1] = '\0';

2. Forgetting to Check NULL

char *p = strchr(str, 'X');
*p = 'Y';  // Crashes if p is NULL

Correction:

if (p) *p = 'Y';

Some students contacted me, wanting to have a study exchange group. I hesitated to create a group before due to concerns about advertisements, but considering that having a group is indeed convenient, I will create one this time to try it out.

If you need it, hurry up and join; the validity period is 7 days.

Comprehensive Summary of C Language String Handling Functions with Specific Usage Examples

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author. Some content and images are sourced from the internet. Please feel free to use them. The views are for learning reference only. If there are any errors or omissions, please forgive me.]

Comprehensive Summary of C Language String Handling Functions with Specific Usage Examples

Comprehensive Summary of C Language String Handling Functions with Specific Usage Examples

“If you like C, please like it”Comprehensive Summary of C Language String Handling Functions with Specific Usage Examples Click the bottom right corner to viewComprehensive Summary of C Language String Handling Functions with Specific Usage Examples

Leave a Comment