logo
  • 現在做什麼
  • 關於我

Kalan

文章分類

  • 前端
  • 開發筆記
  • 雜談
  • 年度回顧

快速連結

  • 現在做什麼
  • 關於我
  • 聯絡我
  • 職涯思考🔗

關注我

在福岡生活的開發者,分享軟體開發與日本生活的點點滴滴。

© 2025 Kalan Made with ❤️. All rights reserved.

String Processing in the C Language

Written byKalanKalanMar 20, 2022
Home/Dev Note
💡

If you have any questions or feedback, pleasefill out this form

Japanese原文

Table of Contents

    This post is translated by ChatGPT and originally written in Mandarin, so there may be some inaccuracies or mistakes.

    In the C programming language, you can use strlen to obtain the length of a string. However, each call to strlen is O(n)O(n)O(n), which can lead to performance bottlenecks in applications that frequently access string operations, especially when dealing with large string lengths in high-traffic applications. One solution is to maintain another variable to store the string length, updating this variable whenever you perform an operation on the string. This way, accessing the string length becomes a matter of retrieving this variable, resulting in a time complexity of O(1)O(1)O(1).

    Another important point to note is that C does not provide any default buffer length, so you need to be very cautious when performing operations like concatenation. For example, consider using strcat as shown below:

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
      char buf1[20] = "abc";
      char buf2[] = "def";
      strcat(buf1, buf2);
      printf("%s\n", buf1);
      return 0;
    }

    This code will concatenate the contents of buf2 to buf1. However, if we reduce the size of buf1 to 5:

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    + char buf1[4] = "abc";
      char buf2[] = "def";
      strcat(buf1, buf2);
    
      printf("%s\n", buf1);
      return 0;
    }

    Running this code will result in an error. This occurs because the combined size of buf1 and buf2 exceeds 4, leading to a buffer overflow. The solution is to check the combined string length before performing the concatenation operation to ensure it will not cause an overflow. If it will, you need to reallocate memory accordingly.

    ← Exploring the form and data retrieval mechanism of remixRewrite the entire blog using Next.js →

    If you found this article helpful, please consider buying me a coffee ☕ It'll make my ordinary day shine ✨

    ☕Buy me a coffee