strlen

PHP身份证验证

房东的猫 提交于 2019-11-28 02:56:53
/** * 身份证号码验证(真正要调用的方法) * @param $id_card 身份证号码 */function validation_filter_id_card($id_card){ if (strlen($id_card) == 18) { $idcard_base = substr($id_card, 0, 17); if (idcard_verify_number($idcard_base) != strtoupper(substr($id_card, 17, 1))) { return false; } else { return true; } } elseif ((strlen($id_card) == 15)) { // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 if (array_search(substr($id_card, 12, 3), array('996', '997', '998', '999')) !== false) { $idcard = substr($id_card, 0, 6) . '18' . substr($id_card, 6, 9); } else { $idcard = substr($id_card, 0, 6) . '19' . substr($id_card, 6, 9); }

hdu2054

大兔子大兔子 提交于 2019-11-28 02:09:30
http://acm.hdu.edu.cn/showproblem.php?pid=2054 很烦的题~~ View Code #include " iostream " #include " string " using namespace std; int i,j,L1,L2; char a[ 20000 ],b[ 20000 ]; int main() { while (scanf( " %s %s " , & a, & b) != EOF) { if (a[ 0 ] == ' - ' && b[ 0 ] == ' - ' || a[ 0 ] != ' - ' && b[ 0 ] != ' - ' ) { int sign =- 1 ; if (a[ 0 ] == ' - ' && b[ 0 ] == ' - ' ) sign = 1 ; else sign = 0 ; int start1 = 0 ,start2 = 0 ,mark1 = 0 ,mark2 = 0 , int end1 = strlen(a),end2 = strlen(b); // ************处理a前面的0************ for (i = sign;i < strlen(a);i ++ ) if (a[i] == ' . ' ) { mark1 = 1 ; break ;} for

Is using strlen() in the loop condition slower than just checking for the null character?

限于喜欢 提交于 2019-11-28 01:49:13
I have read that use of strlen is more expensive than such testing like this: We have a string x 100 characters long. I think that for (int i = 0; i < strlen(x); i++) is more expensive than this code: for (int i = 0; x[i] != '\0'; i++) Is it true? Maybe the second code will not work in some situation so is it better to use the first? Will it be better with the below? for (char *tempptr = x; *tempptr != '\0'; tempptr++) for (int i=0;i<strlen(x);i++) This code is calling strlen(x) every iteration. So if x is length 100, strlen(x) will be called 100 times. This is very expensive. Also, strlen(x)

strlen,sizeof,wcslen

我们两清 提交于 2019-11-28 00:55:44
sizeof和strlen的区别 strlen计算字符串的长度,以’\0’为字符串结束标志 sizeof是分配的数组实际所占的内存空间大小,不受里面存储内容 wcslen计算宽字符串的长度,以’\0’为字符串结束标志 来源: https://blog.csdn.net/weixin_41481284/article/details/99866559

java将数据写入到txt文件中(txt有固定的格式)

﹥>﹥吖頭↗ 提交于 2019-11-27 23:48:24
java将数据写入到txt文件中,这个应该对于学过java I/O的人来说是很简单的事情了,但是如果要将数据以固定的格式写入到txt文件中,就需要一定的技巧了。 这里举个简单的例子,以供参考: 比如我要将数据写成下面的样子: 1 | 2 | 3 | 4 5 | 6 | 8 | 9 也许看起来很简单的,因为每个数据所代表的长度是不一样的,也有可能编码不一样,所有在你看来很简单的事情就不应那么容易实现了。必须要每一个竖线和他下面的竖线是对其的。 这里就不再将写数据的过程提出来了,重点是让每一列的数据都能有一个同样的宽度: public String formatStr(String str, int length) { str = " "+str; int strLen = str.getBytes().length; if (strLen < length) { int temp = length - strLen; for (int i = 0; i < temp; i++) { str += ",| "; } } return str ; }    先讲解一个这个方法的作用:对于参数str而言,如果他的字节长度小于参数length的话,就用空格补齐。 这样做,有个问题,就是怎么确定length的长度为多少合适呢? 如果数据是从数据库中查找出来的

strlen() and UTF-8 encoding

只谈情不闲聊 提交于 2019-11-27 22:22:00
Assuming UTF-8 encoding, and strlen() in PHP, is it possible that this string has a length of 4? I'm only interested to know about strlen(), not other functions This is the string: $1�2 I have tested it on my own computer, and I have verified UTF-8 encoding, and the answer I get is 6. I don't see anything in the manual for strlen or anything I've read on UTF-8 that would explain why some of the characters above would count for less than one. PS: This question and answer (4) comes from a mock test for ZCE I bought on Ebay. The string you posted is six character long: $1�2 (dollar sign,

运算符2

流过昼夜 提交于 2019-11-27 20:59:06
补充上一部分的运算符 这次写了这一些运算符 等于== 赋值= 箭头-> 括号运算符 () 输出运算符>> 1 #include<iostream> 2 #include<string> //string类 存放字符串 3 using namespace std; 4 class myString 5 { 6 char*str;//指针 用于存放字符串 7 int size;//用来管理堆内存的大小 判断内存是否足够 8 public: 9 myString() 10 { 11 size = 1; 12 str = new char[1]{'\0'};//放入一个'\0' 13 } 14 myString(char* string)//拷贝构造 15 { 16 size = strlen(string) + 1; //+1的目的是存放\0 17 str = new char[strlen(string) + 1]; 18 strcpy(str, string);//拷贝过程 19 } 20 ~myString() 21 { 22 if (str != nullptr) 23 { 24 delete[] str; 25 str = nullptr; 26 } 27 } 28 friend bool operator==(const myString&a, const char*b)/

字符串和字符数组

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 20:13:11
1、字符串 严格意义上来说,C语言并没有字符串原生的字符串的类型,而是通过字符指针来实现的:char *p = "LINUX";。而对于其他的高级语言来说,比如C++的就存在字符串类型: string p1 = "I LOVE LINUX"; 。 字符串在内存中其实就多个字节组成的,且地址都是连续的;指针指向字符串的的头部;字符串的结尾是以"\0"(是真能整的零)作为字符串结束的标志。 "\0" 的理解: \0 是一个 ASCII 字符,其实是编码为零的字符,是真正的零。这个和数字零是不同的,数字零的 ASCII 是48。 注意: 当定义一个字符串的时候:char *p = "LINUX",系统会在这个字符串的结尾处 自动添加'\0' ,作为结束的标志,所以'\0'虽然是连接这最后一个字符,但是并不属于这个字符串。 分配的字符串,是被保存到文字常量区 int main(int argc, char *argv[]) { char *p = "I LOVE LINUX"; printf("%c\n", *p); printf("%s\n", p); while (1); } 输出结果为: II LOVE LINUX 2、字符数组 定义:char p[] = "I LOVE LINUX"; 其实就是将字符串放到数组中,在一块连续的内存中依次排放,则数组名则是指向字符串第一个地府的地址

字符数组和字符串数组

醉酒当歌 提交于 2019-11-27 20:12:21
demo int _tmain(int argc, _TCHAR* argv[]) { char a[5] = {1,2,3,4,5};  //字符数组 char b[5] = {1,2,3,4,'\0'};  //字符数组,也可以叫字符串数组;a[5]不可以叫字符串数组 char c[5] = "test";  //字符数组,也可以叫字符串数组,a[5]不可以叫字符串数组 int arry[5] = {10000,2147483647,2147483647,2147483647,2147483647};  //整型数组 getchar(); return 0; } - a 0x00cffc70 "烫烫烫烫烫蘆" char [5] [0] 1 '' char [1] 2 '' char [2] 3 '' char [3] 4 '' char [4] 5 '' char strlen(a) 18 int sizeof(a) 5 unsigned int - b 0x00cffc60 "" char [5] [0] 1 '' char [1] 2 '' char [2] 3 '' char [3] 4 '' char [4] 0 char strlen(b) 4 int sizeof(b) 5 unsigned int - c 0x00cffc50 "test" char [5] [0

C strings, strlen and Valgrind

邮差的信 提交于 2019-11-27 18:19:19
问题 I'm trying to understand why Valgrind is spitting out : ==3409== Invalid read of size 8 ==3409== at 0x4EA3B92: __GI_strlen (strlen.S:31) whenever I'm applying strlen on a dynamically allocated string? Here is a short testcase : #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *hello = "Hello World"; char *hello2; /* Step 1 */ printf("Step 1\n"); printf("strlen : %lu\n",(unsigned long)strlen(hello)); /* Step 2 */ hello2 = calloc(12,sizeof(char)); hello2[0] = 'H';