The str function is a commonly used string processing function in C language. It is used to perform various operations, including: concatenating strings (strcat) comparing strings (strcmp) copying strings (strcpy) and calculating string length. (strlen)
Usage of str in C language
Introduction:
str is one of the most commonly used string processing functions in C language. It allows manipulating null-terminated character arrays (strings) and performing various operations.
Syntax:
<code class="c">char *str(const char *str1, const char *str2);</code>
Parameters:
Return value:
Returns a pointer to the result string.
Usage:
The str function can be used for various string operations, including:
Example:
<code class="c">#include <stdio.h> #include <string.h> int main() { char str1[] = "Hello"; char str2[] = "World"; // 字符串连接 strcat(str1, " "); strcat(str1, str2); printf("连接后的字符串:%s\n", str1); // 字符串比较 int result = strcmp(str1, "Hello World"); if (result == 0) { printf("字符串相等\n"); } else if (result > 0) { printf("str1 大于 str2\n"); } else { printf("str1 小于 str2\n"); } // 字符串复制 strcpy(str1, str2); printf("复制后的字符串:%s\n", str1); // 字符串长度 int length = strlen(str1); printf("字符串长度:%d\n", length); return 0; }</code>
Output:
<code>连接后的字符串:Hello World 字符串相等 复制后的字符串:World 字符串长度:5</code>
The above is the detailed content of How to use str in c language. For more information, please follow other related articles on the PHP Chinese website!