strlen

Openjudge 1.7 字符串

孤者浪人 提交于 2019-12-06 15:03:27
1.7.9 字母后移 读入一行的代码: while((k=getchar())!=EOF) ASCII码 小写 \(a\) 97 小写 \(z\) 122 大写 \(A\) 65 大写 \(Z\) 90 数字0 \(48\) 数字9 \(57\) //1.7.9 #include<bits/stdc++.h> using namespace std; int main() { char p[85]; char k; int i=0; while((k=getchar())!=EOF){ if(k>=65&&k<=89) k++; else if(k==90) k=65; else if(k>=97&&k<=121) k++; else if(k==122) k=97; //else if(k==32) putchar('32'); p[i]=k; i++; putchar(k); } //puts(p); return 0; } 1.7.11 潜伏者 //1.7.11? #include <algorithm> #include <iostream> #include <cmath> #include <cstdio> #include <cstring> using namespace std; #define N 1020 char s1[N],s2[N],s3[N]; int

PHP提交表单验证全方面

一世执手 提交于 2019-12-06 11:41:49
对一个接收自由提交表单数据的文件进行安全性分析,希望对各位有帮助。首先说明一下,代码中的error()和succeed()是我自定义的函数,用于显示错误信息和成功信息,其实也可以直接echo出错误信息,这里我只是想我的出错信息页面漂亮点,定义了一个页面输出的函数罢了。 <?php // savecomment.php// 大家先不要看注释,看完本文后,再回过头来看 require ("config.php"); mysql_connect($servername,$dbusername,$dbpassword) or die ("数据库连接失败"); $name=$HTTP_POST_VARS[';name';]; $content=$HTTP_POST_VARS[';content';]; $blogid=$HTTP_POST_VARS[';blogid';]; $datearray=getdate(time()); $date=date("Y-m-d h:i:s",$datearray[0]); if (!empty($name) && !empty($content)){ //用empty函数判断表单非空的话则往下。 if(strlen($name) > 20){ //通过非空判断则开始判断$name的长度。 error(“名字超过20个字节(20个英文或10个汉字)<br>

C char pointer length

纵然是瞬间 提交于 2019-12-06 09:06:30
问题 This was a quiz (not graded) on Coursera. The question was, what does the following code possibly evaluate to? The correct answers were 127 and 0 (other options were crash, -1, 128. Why does the following code possibly evaluate to 0? I understand why it would evaluate to 127. Is it just as simple as the char bytes are uninitialized and therefore random? Can it also possibly evaluate to any # between 0 and 127? int foo(void) { char bar[128]; char *baz = &bar[0]; baz[127] = 0; return strlen(baz

strlen

戏子无情 提交于 2019-12-06 04:13:27
strlen (C/C++语言函数) strlen所作的是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值(长度不包含'\0')。 中文名 strlen 头文件 string.h(C)或cstring(C++) 格 式 strlen(字符指针表达式) 功 能 计算给定 字符串 的长度 实 质 C/C++语言函数 返回值 给定字符串(不包括“\0”)长度 strlen函数原型 1 extern unsigned int strlen ( char *s); 1 size_t strlen ( const char *string); ,其中size_t实际上是unsigned int,在VC6.0或Dev-C++中可以看到这样的代码: 1 typedef unsigned int size_t ; 头文件 :string.h或cstring 格式: strlen (字符指针表达式) 功能 :计算给定 字符串 的(unsigned int型)长度,不包括'\0'在内 说明 :返回s的长度,不包括结束符NULL。 相关函数: 1 TCHAR .H routine _UNICODE & _MBCS not defined_MBCS defined_UNICODE

Different ways to calculate string length

一曲冷凌霜 提交于 2019-12-06 04:06:17
A comment on one of my answers has left me a little puzzled. When trying to compute how much memory is needed to concat two strings to a new block of memory, it was said that using snprintf was preferred over strlen , as shown below: size_t length = snprintf(0, 0, "%s%s", str1, str2); // preferred over: size_t length = strlen(str1) + strlen(str2); Can I get some reasoning behind this? What is the advantage, if any , and would one ever see one result differ from the other? R.. I was the one who said it, and I left out the +1 in my comment which was written quickly and carelessly, so let me

How do I find the number of bytes within UTF-8 string with PHP?

拜拜、爱过 提交于 2019-12-06 01:43:08
问题 I have the following function from the php.net site to determine the # of bytes in an ASCII and UTF-8 string: <?php /** * Count the number of bytes of a given string. * Input string is expected to be ASCII or UTF-8 encoded. * Warning: the function doesn't return the number of chars * in the string, but the number of bytes. * * @param string $str The string to compute number of bytes * * @return The length in bytes of the given string. */ function strBytes($str) { // STRINGS ARE EXPECTED TO BE

php打包图片后进行批量下载

旧街凉风 提交于 2019-12-05 22:19:41
1 <?php 2 3 4 class Imagedown { 5 var $datasec = array (); 6 var $ctrl_dir = array (); 7 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; 8 var $old_offset = 0; 9 public function unix2_dostime($unixtime = 0){ 10 $timearray = ($unixtime == 0) ? getdate () : getdate($unixtime); 11 if ($timearray ['year'] < 1980){ 12 $timearray ['year'] = 1980; 13 $timearray ['mon'] = 1; 14 $timearray ['mday'] = 1; 15 $timearray ['hours'] = 0; 16 $timearray ['minutes'] = 0; 17 $timearray ['seconds'] = 0; 18 } 19 return (($timearray ['year'] - 1980) << 25) | ($timearray ['mon'] << 21) | ($timearray ['mday']

KMP

被刻印的时光 ゝ 提交于 2019-12-05 19:39:21
简介: KMP算法,适用于模式匹配,即查找模式串P在字符串S内的出现位置,其时间复杂度为O(M+N)。 (题外话:模式匹配问题是算法竞赛的常客,但理解KMP算法具有一定难度,建议先理解BF算法后再理解KMP算法。当然,网上关于KMP算法的讲解很多,这里仅仅只是给出模板。) 模板: 下面给出2种求next数组的方法,其中优化next数组求法虽然使得kmp算法更快,但针对需要输出模式串p的next数组的算法题,往往多数都是要原方法求出next数组。两种方法求出的next数组有什么区别可以通过网上关于KMP算法的讲解了解到。 1 //求出模式串p的next数组 2 void Next(char* p,int *next) 3 { 4 int pLen = strlen(p); 5 next[0] = -1; 6 int k = -1; 7 int j = 0; 8 9 while (j < pLen){ 10 //p[k]表示前缀,p[j]表示后缀 11 if (k == -1 || p[k] == p[j]) { 12 ++k; 13 ++j; 14 next[j] = k; 15 }else{ 16 k = next[k]; 17 } 18 } 19 } 1 //优化next数组求法 2 void Next(char* p, int *next) 3 { 4 int pLen =

A1060 Are They Equal (25 分)

喜夏-厌秋 提交于 2019-12-05 09:43:27
一、技术总结 cnta、cntb用于记录小数点出现的位置下标,初始化为strlen(字符串)长度。 q、p用于记录第一个非0(非小数点)出现的下标,可以用于计算次方和方便统计输出的字符串,考虑到前面可能出现0。 如果cnta > p ,说明小数点在第一个开始的非0数的下标的右边,那么科学计数法的指数为cnta – p ; 否则应该为cnta – p + 1; 字符串b同理 如果字符串p、q等于字符串长度,说明字符串为0,此时直接把 cnta(或者cntb)置为0,因为对于 0来说乘以几次方都是相等的,如果不置为0可能会出现两个0比较导致判断为它们不相等 indexa = 0开始给新的A数组赋值,共赋值n位除去小数点外的正常数字,从p的下标开始。如果p 大于等于strlen,说明字符串遍历完毕后依旧没能满足需要的位数,此时需要在A数组后面补上0 直到满足n位数字。indexb同理,产生新的B数组 判断A和B是否相等,且cnta和cntb是否相等。如果相等,说明他们用科学计数法表示后是相同的,输出YES,否则输出NO,同时输出正确的科学计数法 数组开大一点 二、参考代码 #include<iostream> #include<cstring> using namespace std; int main(){ int n, p = 0, q = 0; char a[10000], b

sizeof与strlen

寵の児 提交于 2019-12-05 09:43:10
#include <stdio.h> #include <string.h> #include<stdlib.h> void testArr(const char str[]) { printf("%lu %lu\n", sizeof(str), strlen(str)); } int main(void) { char str[] = "hello"; printf("test0 %lu %lu\n\n", sizeof(str), strlen(str)); char str1[8] = "hello"; printf("test1 %lu %lu\n\n", sizeof(str1), strlen(str1)); char str2[] = { 'h','e','l','l','o' }; printf("test2 %lu %lu\n\n", sizeof(str2), strlen(str2)); char *str3 = "hello"; printf("test3 %lu %lu\n\n", sizeof(str3), strlen(str3)); char str4[] = "hello"; testArr(str4); char str5[] = "hell\0o"; printf("test5 %lu %lu\n", sizeof(str5), strlen