Vuser中常用的C语言函数
strchr函数与strrchr函数查找指定字符(仅能判断字符,不能是字符串)在字符串中的位置
1 Action() 2 { 3 // strchr函数用于查找指定字符在一个字符串中第1次出现的位置,然后返回指向该位置的指针; 4 // strrchr函数用户查找指定字符在一个字符串中最后1次出现的位置,然后返回指向该位置的指针。 5 // 语法结构 6 // char *strchr(const char *string,int c); 7 // char *strrchr(const char *string,int c); 8 9 10 char *string = "I'm a young soul in this very strange world"; 11 char *first_a, *last_a; 12 13 first_a = (char *)strchr(string, 'a'); 14 lr_output_message("The first occurrence of a: %s", first_a); 15 16 last_a = (char *)strrchr(string, 'a'); 17 lr_output_message("The last occurrence of a: %s", last_a); 18 19 return 0; 20 } 21 ----------------------------------------------------------------------------------- 22 Running Vuser... 23 Starting iteration 1. 24 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 25 Starting action Action. 26 Action.c(14): The first occurrence of a: a young soul in this very strange world 27 Action.c(17): The last occurrence of a: ange world 28 Ending action Action. 29 Ending iteration 1. 30 Ending Vuser...
strcpy函数与strncpy函数复制字符串
1 Action() 2 { 3 // strcpy函数是把一个字符串复制到另一个字符串中; 4 // strncpy函数是把一个字符串中的指定长度复制到另一个字符串中,如果指定长度超过了字符串长度,则会复制整个字符串。 5 // 语法结构 6 // strcpy: char *strcpy(char *dest, const char *source); 7 // strncpy: char *strncpy(char *dest,const char *source, size_t n); 8 // 参数dest是目标字符串,source是源字符串,n是要复制的长度。 9 10 11 char test[50]; 12 char ntest[50]; 13 strcpy(test, "Copies one string to another."); 14 lr_output_message("%s", test); 15 16 strncpy(ntest, "Copies the first n characters of one string to another?", 30); 17 lr_output_message("%s", ntest); 18 19 return 0; 20 } 21 22 ---------------------------------------------------------------------------------- 23 Running Vuser... 24 Starting iteration 1. 25 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 26 Starting action Action. 27 Action.c(14): Copies one string to another. 28 Action.c(17): Copies the first n characters 29 Ending action Action. 30 Ending iteration 1. 31 Ending Vuser...
strcmp函数与stricmp函数用于按照字母表顺序来比较两个字符串的大小
1 Action() 2 { 3 // strcmp函数与stricmp函数用于按照字母表顺序来比较两个字符串的大小,前者比较时区分字符串的大小写,后者比较时不区分字符串的大小写 4 // 语法结构 5 // strcmp: int strcmp(const char *string1, const char *string2); 6 // stricmp: int stricmp(const char *string1, const char *string2); 7 8 int result; 9 char tmp[20]; 10 char string1[] = "He is a test"; 11 char string2[] = "He is a TEST"; 12 13 // strcmp区分大小写 14 result = strcmp(string1, string2); 15 if(result > 0)strcpy(tmp, "greater than"); 16 else if(result < 0)strcpy(tmp, "less than"); 17 else strcpy(tmp, "equal to"); 18 19 lr_output_message("strcmp: String1 is %s string2", tmp); 20 21 // stricmp不区分大小写 22 result = stricmp(string1, string2); 23 if(result > 0)strcpy(tmp, "greater than"); 24 else if(result < 0)strcpy(tmp, "less than"); 25 else strcpy(tmp, "equal to"); 26 27 lr_output_message("stricmp: String1 is %s string2", tmp); 28 29 return 0; 30 } 31 32 --------------------------------------------------------------------------------------- 33 Running Vuser... 34 Starting iteration 1. 35 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 36 Starting action Action. 37 Action.c(19): strcmp: String1 is greater than string2 38 Action.c(27): stricmp: String1 is equal to string2 39 Ending action Action. 40 Ending iteration 1. 41 Ending Vuser...
strcat函数与strncat函数都是字符串连接函数
1 Action() 2 { 3 // strcat函数与strncat函数都是字符串连接函数; 4 // strcat:把一个字符串全部内容复制到另一个字符串后面; 5 // strncat:把一个字符串的指定长度复制到另一个字符串的后面,如果指定长度超过了字符串的长度,则会复制整个字符串。 6 // 语法结构: 7 // strcat: char *strcat(char *to_string,const char *from_string); 8 // strncat: char *strncat(char *to_string,const char *from_string, size_t n); 9 10 char str1[] = "This is "; 11 char str2[] = "a "; 12 char str3[] = "strcat&strncat "; 13 char str4[] = "test!"; 14 lr_output_message("The str1 is: %s", str1); 15 16 strcat(str1, str2); 17 lr_output_message("The str1 is: %s", str1); 18 19 strncat(str1, str3, 100); 20 lr_output_message("The str1 is: %s", str1); 21 22 strcat(str1, str4, 5); 23 lr_output_message("The str1 is: %s", str1); 24 25 return 0; 26 } 27 28 ------------------------------------------------------------------------- 29 Starting iteration 1. 30 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 31 Starting action Action. 32 Action.c(14): The str1 is: This is 33 Action.c(17): The str1 is: This is a 34 Action.c(20): The str1 is: This is a strcat&strncat 35 Action.c(23): The str1 is: This is a strcat&strncat test! 36 Ending action Action.
sprintf函数:把一个字符串格式化后输入到另一个字符串中,然后返回写入的字符数量
1 Action() 2 { 3 // sprintf函数:把一个字符串格式化后输入到另一个字符串中,然后返回写入的字符数量 4 // 语法结构: 5 // sprintf: int sprintf(char *string,const char *format_string[,args]); 6 7 char test[30]; 8 int a=2,b=8; 9 sprintf(test, "Add Test: a=%d,b%d,a+b=%d.\0", a, b, a+b); // 字符串中的“\0”是字符串结束符。 10 lr_output_message(test); 11 12 return 0; 13 } 14 15 ------------------------------------------------------------------------- 16 Starting iteration 1. 17 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 18 Starting action Action. 19 Action.c(10): Add Test: a=2,b8,a+b=10. 20 Ending action Action. 21 Ending iteration 1.
strlwr函数:把一个字符串全部变小写;
strupr函数:把一个字符串全部变大写;
1 Action() 2 { 3 // strlwr函数:把一个字符串全部变小写; 4 // strupr函数:把一个字符串全部变大写; 5 // 语法结构: 6 // strlwr: char *strlwr(char *string); 7 // strupr: char *strupr(char *string); 8 9 char str[] = "ThIS is A tEst."; 10 char *lowerstr, *upperstr; 11 12 lowerstr = (char *)strlwr(str); 13 lr_output_message("lowerstr=%s", lowerstr); 14 lr_output_message("str=%s", str); 15 16 upperstr = (char *)strupr(str); 17 lr_output_message("upperstr=%s", upperstr); 18 lr_output_message("str=%s", str); 19 20 return 0; 21 } 22 23 ------------------------------------------------------------------------- 24 Starting iteration 1. 25 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 26 Starting action Action. 27 Action.c(13): lowerstr=this is a test. 28 Action.c(14): str=this is a test. 29 Action.c(17): upperstr=THIS IS A TEST. 30 Action.c(18): str=THIS IS A TEST. 31 Ending action Action. 32 Ending iteration 1.
atoi函数:把一个字符串转为整数;
itoa函数:把整数转为字符串;
1 Action() 2 { 3 // atoi函数:把一个字符串转为整数; 4 // itoa函数:把整数转为字符串; 5 // 语法结构: 6 // atoi: int atoi(const char *string); 7 // itoa: int itoa(int value,char *string,int radix); 8 // 参数value是要转换的整数,string是用例保存转换结果的字符串,radix是转换时的进制。 9 10 int j, i; 11 char *s = "aaa 111 bbb 222"; 12 char str_int[10], str_hex[10]; 13 14 j = atoi(s); 15 lr_output_message("Result:%d", j); 16 17 s = "111 bbb 222"; 18 j = atoi(s); 19 lr_output_message("Result:%d", j); 20 21 22 i=56; 23 itoa(i, str_int, 10); 24 lr_output_message("New file name %s", str_int); 25 26 itoa(i, str_hex, 16); 27 lr_output_message("New file name %s", str_int); 28 29 return 0; 30 } 31 32 ------------------------------------------------------------------------- 33 Starting iteration 1. 34 Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] 35 Starting action Action. 36 Action.c(15): Result:0 37 Action.c(19): Result:111 38 Action.c(24): New file name 56 39 Action.c(27): New file name 56 40 Ending action Action. 41 Ending iteration 1.