· 2 min read

String Processing in the C Language

# Dev Note
This article was auto-translated from Chinese. Some nuances may be lost in translation.

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), 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).

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.