strlen

What if a null character is present in the middle of a string?

烈酒焚心 提交于 2021-02-18 16:57:46
问题 I understand that the end of a string is indicated by a null character, but i cannot understand the output of the following code. #include <stdio.h> #include <string.h> int main(void) { char s[] = "Hello\0Hi"; printf("%d %d", strlen(s), sizeof(s)); } OUTPUT: 5 9 If strlen() detects the end of the string at the end of o, then why doesn't sizeof() do the same thing? Even if it doesn't do the same thing, isn't '\0' A null character (i.e, only one character), so shouldn't the answer be 8? 回答1:

How to use calloc() in C?

余生颓废 提交于 2021-02-16 19:02:50
问题 Shouldn't I get an error if my string goes over 9 characters long in this program? // CString.c // 2.22.11 #include <stdio.h> #include <stdlib.h> #include <string.h> main() { char *aString = calloc(10, sizeof(char)); if (aString == NULL) { return 1; } printf("PLEASE ENTER A WORD: "); scanf("%s", aString); printf("YOU TYPED IN: %s\n", aString); //printf("STRING LENGTH: %i\n", strlen(aString)); } Thanks blargman 回答1: You don't get a compiler error because the syntax is correct. What is

strlen() gives wrong size cause of null bytes in array

情到浓时终转凉″ 提交于 2021-02-08 12:11:46
问题 I have a dynamic char array that was deserialized from a stream. Content of char *myarray on the file (with a hexal editor) : 4F 4B 20 31 32 20 0D 0A 00 00 B4 7F strlen(myarray) returns 8, (must be 12) 回答1: strlen(myarray) returns the index of the first 00 in myarray . 回答2: strlen counts the characters up to the first 0 character, that's what it's for. If you want to know the length of the deserialized array, you must get that from somewhere else, the deserialization code should know how

getting the length of an array using strlen in g++ compiler

淺唱寂寞╮ 提交于 2021-02-04 18:55:07
问题 could someone explain why i am getting this error when i am compiling the source using following g++ compiler #include <cstdio> #include <string> using namespace std; int main() { char source_language[50]; scanf("%16s\n",source_language); int length = sizeof(source_language); int sizeofchar = strlen(source_language); printf("%d\n",sizeofchar); } this gives me following error test.cpp: In function ‘int main()’: test.cpp:31: error: ‘strlen’ was not declared in this scope when i change the

Why Null character does not adds up to the end of string when scanf'd?

浪子不回头ぞ 提交于 2020-05-28 11:55:07
问题 --start of snip-- char name[15]; ... printf("Enter employee name \n"); scanf("%s",name); printf("strlen %d \n", strlen(name)); --end of snip -- Output: Enter employee name Veronica 8 why is it not adding null character to the end !? am i missing anything? Please someone explain. Edited: Was reading line from opened file using fgets and used strtok(line,"\t ") to get the tokens from the line. --snip-- char * chk; char line[100]; char temp_name[15]; while(fgets(line, sizeof line, filep)) { chk

Why does this REPNE SCASB implementation of strlen work?

房东的猫 提交于 2020-05-09 07:41:06
问题 Why does this code work? http://www.int80h.org/strlen/ says that the string address has to be in EDI register for scasb to work, but this assembly function doesn't seem to do this. Assembly code for mystrlen : global mystrlen mystrlen: sub ecx, ecx not ecx sub al, al cld repne scasb neg ecx dec ecx dec ecx mov eax, ecx ret C main: int mystrlen(const char *); int main() { return (mystrlen("1234")); } Compilation: nasm -f elf64 test.asm gcc -c main.c gcc main.o test.o Output: ./a.out echo $? 4

strlen和mb_strlen的区别

梦想与她 提交于 2020-04-07 12:05:29
在 PHP 中, strlen 与 mb_strlen 是求字符串长度的函数,但是对于一些初学者来说,如果不看手册,也许不太清楚其中的区别。 下面通过例子,讲解这两者之间的区别。 先看例子: <?php //测试时文件的编码方式要是UTF8 $str='中文a字1符'; echo strlen($str).'<br>';//14 echo mb_strlen($str,'utf8').'<br>';//6 echo mb_strlen($str,'gbk').'<br>';//8 echo mb_strlen($str,'gb2312').'<br>';//10 ?> 结果分析:在strlen计算时,对待一个UTF8的中文字符是3个长度,所以“中文a字1符”长度是3*4+2=14,在mb_strlen计算时,选定内码为UTF8,则会将一个中文字符当作长度1来计算,所以“中文a字1符”长度是6 . 利用这两个函数则可以联合计算出一个中英文混排的串的占位是多少(一个中文字符的占位是2,英文字符是1) echo (strlen($str) + mb_strlen($str,'UTF8')) / 2; 例如 “中文a字1符” 的strlen($str)值是14,mb_strlen($str)值是6,则可以计算出“中文a字1符”的占位是10. echo mb_internal

strlen和mb_strlen的区别

≡放荡痞女 提交于 2020-04-07 12:05:11
在php中常见的计算字符串长度的函数有:strlen和mb_strlen.当字符全是英文字符的时候,两者是一样。这里主要比较一下,中英文混排的时候,两个计算结果。 AD: 2013云计算架构师峰会超低价抢票中 在 PHP 中, strlen 与 mb_strlen 是求字符串长度的函数,但是对于一些初学者来说,如果不看手册,也许不太清楚其中的区别。 下面通过例子,讲解这两者之间的区别。 先看例子: <?php //测试时文件的编码方式要是UTF8 $str = '中文a字1符' ; echo strlen ( $str ). '<br>' ; //14 echo mb_strlen( $str , 'utf8' ). '<br>' ; //6 echo mb_strlen( $str , 'gbk' ). '<br>' ; //8 echo mb_strlen( $str , 'gb2312' ). '<br>' ; //10 ?> 结果分析:在strlen计算时,对待一个UTF8的中文字符是3个长度,所以“中文a字1符”长度是3*4+2=14,在mb_strlen计算时,选定内码为UTF8,则会将一个中文字符当作长度1来计算,所以“中文a字1符”长度是6 . 利用这两个函数则可以联合计算出一个中英文混排的串的占位是多少(一个中文字符的占位是2,英文字符是1) echo (

几个支持中文的PHP字符串截取函数

给你一囗甜甜゛ 提交于 2020-04-07 10:55:24
字符串截取是一个非常常见的编程任务,而往往带中文的字符串截取会经常用到。虽然不难,但是自己写函数实现又耗费时间,这里介绍一个比较好用的字符串截取函数,能够胜任基本的需求了。 <?php function sysSubStr($string,$length,$append = false) { if(strlen($string) <= $length ) { return $string; } else { $i = 0; while ($i < $length) { $stringTMP = substr($string,$i,1); if ( ord($stringTMP) >=224 ) { $stringTMP = substr($string,$i,3); $i = $i + 3; } elseif( ord($stringTMP) >=192 ) { $stringTMP = substr($string,$i,2); $i = $i + 2; } else { $i = $i + 1; } $stringLast[] = $stringTMP; } $stringLast = implode("",$stringLast); if($append) { $stringLast .= "..."; } return $stringLast; } } $string =

Where is the source code for strlen() function in PHP?

Deadly 提交于 2020-04-06 08:50:37
问题 I was looking through php-src/Zend/zend_API.c and couldn't find the source code for the strlen() function in PHP anywhere. Grepping through the code base didn't really help as it's littered with libc strlen everywhere. Googling doesn't much help either. I tried using the Vulcan Logic Dumper extension to inspect what's going on under the hood. I tried the following code as a test: <?php strlen("foo"); strpos("foo", "f"); This is what I got: Finding entry points Branch analysis from position: 0