常见str*型函数用法总结

最近看到了csapp的网络编程,书上给了一个简单编写一个web服务器的代码,在解析客户端请求的一个函数中中用到了很多str*型的函数,看的我很迷糊,特此总结一下,加深记忆也方便以后来看.

  • strcmp函数
    • Description: this function compares the string pointed to, by str1 to the string pointed to by str2.
    • Declaration: int strcmp(const char *str1, const char *str2)
    • Parameters:
      • str1 – This is the first string to be compared.
      • str2 – This is the second string to be compared.
    • Return value: This function return values that are as follows:
      • if Return value < 0 then it indicates str1 is less than str2.
      • if Return value > 0 then it indicates str2 is less than str1.
      • if Return value = 0 then it indicates str1 is equal to str2.
  • strstr函数
    • Description: this function finds the first occurrence of the substring needle in the string haystack. The teminating ‘\0’ character is not compared.
    • Declaration: char *strstr(const char *haystack, const char *needle)
    • Parameters:
      • haystack – This is the main C string to be scanned.
      • needle – This is the small string to be searched with-in haystack string.
    • Return value: This function returns a pointer to the first occurrence in haystack of any of the entire sequence of characters specified in needle, or a null pointer if the sequence is not present in haystack.
  • strcpy函数
    • Description: this function copies the string pointed to, by src to dest.
    • Declaration: char *strcpy(char *dest, const char *src)
    • Parameters:
      • dest – This is the pointer to the destination array where the content is to be copied.
      • src – This is the string to be copied.
    • Return value: This returns a pointer to the destination string dest.
  • strcat函数
    • Description: this function appends the string pointed to by src to the end of the string pointed to by dest.
    • Declaration: char *strcat(char *dest, const char *src)
    • Parameters:
      • dest – This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.
      • src – This is the string to be appended. This should not overlap the destination.
    • Return value: This function returns a pointer to the resulting string dest.
  • strchr函数
    • Description: this function searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str.
    • Declaration: char *strchr(const char *str, int c)
    • Parameters:
      • str – This is the C string to be scanned.
      • c – This is the character to be searched in str.
    • Return value: This returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found.