在C程序中使用字符串,不可以使用操作符来操作字符串,应该使用一组标准函数,C标准库中有对于使用字符串操作的一组函数(需要包含头文件string.h)
size_t strlen(const char *str)
参数
#include
#include
int main ()
{
char str[50];
int len;
strcpy(str, "This is yiibai.com");
len = strlen(str);
printf("Length of |%s| is |%d|
", str, len);
return(0);
}
Length of |This is yiibai.com| is |26|
char *strcat(char *dest, const char *src)
参数
#include
#include
int main ()
{
char src[50], dest[50];
strcpy(src, "This is source");
strcpy(dest, "This is destination");
strcat(dest, src);
printf("Final destination string : |%s|", dest);
return(0);
}
Final destination string : |This is destinationThis is source|
char *strncat(char *dest, const char *src, size_t n)
参数
#include
#include
int main ()
{
char src[50], dest[50];
strcpy(src, "This is source");
strcpy(dest, "This is destination");
strncat(dest, src, 15);
printf("Final destination string : |%s|", dest);
return(0);
}
Final destination string : |This is destinationThis is source|
int strcmp(const char *str1, const char *str2)
参数
#include
#include
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret > 0)
{
printf("str1 is less than str2");
}
else if(ret < 0)
{
printf("str2 is less than str1");
}
else
{
printf("str1 is equal to str2");
}
return(0);
}
str1 is less than str2
int strncmp(const char *str1, const char *str2, size_t n)
参数
#include
#include
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strncmp(str1, str2, 4);
if(ret > 0)
{
printf("str1 is less than str2");
}
else if(ret < 0)
{
printf("str2 is less than str1");
}
else
{
printf("str1 is equal to str2");
}
return(0);
}
str1 is less than str2
char *strcpy(char *dest, const char *src)
参数
#include
#include
int main()
{
char src[40];
char dest[100];
memset(dest, '', sizeof(dest));
strcpy(src, "This is yiibai.com");
strcpy(dest, src);
printf("Final copied string : %s
", dest);
return(0);
}
Final copied string : This is yiibai.com
char *strncpy(char *dest, const char *src, size_t n)
参数
#include
#include
int main()
{
char src[40];
char dest[12];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is runoob.com");
strncpy(dest, src, 10);
printf("最终的目标字符串: %s\n", dest);
return(0);
}
最终的目标字符串: This is ru
void *memset(void *str, int c, size_t n)
参数
#include
#include
int main ()
{
char str[50];
strcpy(str,"This is string.h library function");
puts(str);
memset(str,'$',7);
puts(str);
return(0);
}
This is string.h library function
$$$$$$$ string.h library function
char *strstr(const char *haystack, const char *needle)
参数
#include
#include
int main()
{
const char haystack[20] = “TutorialsYiibai”;
const char needle[10] = “Yiibai”;
char *ret;
ret = strstr(haystack, needle);
printf("The substring is: %s
", ret);
return(0);
}
The substring is: Yiibai
具体参考:https://www.yiibai.com/c_standard_library/c_function_sprintf.html
#include
#include
int main()
{
char str[80];
sprintf(str, "Value of Pi = %f", M_PI);
puts(str);
return(0);
}
Value of Pi = 3.141593
具体参考:https://www.yiibai.com/c_standard_library/c_function_sscanf.html
#include
#include
int main()
{
int day, year;
char weekday[20], month[20], dtm[100];
strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );
printf("%s %d, %d = %s
", month, day, year, weekday );
return(0);
}
March 25, 1989 = Saturday