strcmp

strcmp函数和memcmp函数的用法区别及联系

核能气质少年 提交于 2019-12-04 20:27:40
  前言:  C语言中有很多东西容易搞混,最近笔者就遇到了一个问题。这里做个记录。就是memcmp和strcmp两者的用法,这里做个对比:   功能对比:  A memcmp: 函数原型: int memcmp(const void *str1, const void *str2, size_t n));  功能:比较内存区域buf1和buf2的前count个字节。  返回值: 如果返回值 < 0,则表示 str1 小于 str2。 如果返回值 > 0,则表示 str2 小于 str1。 如果返回值 = 0,则表示 str1 等于 str2。  B strcmp函数  函数原型: int strcmp ( const char *s1, const char *s2);  功能:用于比较两个字符串并根据比较结果返回整数  返回值: 如果返回值 < 0,则表示 str1 小于 str2。 如果返回值 > 0,则表示 str2 小于 str1。 如果返回值 = 0,则表示 str1 等于 str2。  源码对比:   下面这个例子,能够很好的诠释两个函数的用法: 1 #include <stdio.h> 2 #include <string.h> 3 4 5 int main() 6 { 7 unsigned char test1_arr[32] = "hello world"; 8

西普 部分WEB Writeup

你。 提交于 2019-12-04 15:56:15
WEB 拐弯抹角 打开一堆代码和注释 ` <?php // code by SEC@USTC echo '<html><head><meta http-equiv="charset" content="gbk"></head><body>' ; $URL = $_SERVER [ 'REQUEST_URI' ]; //echo 'URL: '.$URL.'<br/>'; $flag = "CTF{???}" ; $code = str_replace( $flag , 'CTF{???}' , file_get_contents( './index.php' )); $stop = 0 ; //这道题目本身也有教学的目的 //第一,我们可以构造 /indirection/a/../ /indirection/./ 等等这一类的 //所以,第一个要求就是不得出现 ./ if ( $flag && strpos( $URL , './' ) !== FALSE ){ $flag = "" ; $stop = 1 ; //Pass } //第二,我们可以构造 \ 来代替被过滤的 / //所以,第二个要求就是不得出现 ../ if ( $flag && strpos( $URL , '\\' ) !== FALSE ){ $flag = "" ; $stop = 2 ; //Pass }

Swift why strcmp of backspace returns -92?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 12:39:23
I was tried to detecting backspace inside of UITextfieldDelegate. And found this answer. https://stackoverflow.com/a/49294870/911528 And It working correctly. But I don't know what's going on inside of this function. let char = string.cString(using: String.Encoding.utf8)! let isBackSpace = strcmp(char, "\\b") if isBackSpace == -92 { print("Backspace was pressed") return false } I don't know why the result of cString of backspace is 0. I can debug on XCode. Please check the screenshot. I don't know why the strcmp about 0 for backspace(as far as I got this value on XCode debugger) and "\b"

How to compare two strings case and diacritic-insensitive?

蹲街弑〆低调 提交于 2019-12-04 12:31:33
问题 I have two strings String 1: "sebastien" String 2: "Sébastien" I want to compare these two strings by ignoring é (Accents) character. Can anyone know this logic? Thanks in advance 回答1: <?php $string_1 = 'sebastien'; $string_2 = 'Sébastien'; function transliterateString($txt) { $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A

Why does “echo strcmp('60', '100');” in php output 5?

天涯浪子 提交于 2019-12-04 10:40:19
问题 PHP's documentation on this function is a bit sparse and I have read that this function compares ASCII values so... echo strcmp('hello', 'hello'); //outputs 0 as expected - strings are equal. echo '<hr />'; echo strcmp('Hello', 'hello'); //outputs -32, a negative number is expected as //uppercase H has a lower ASCII value than lowercase h. echo '<hr />'; echo strcmp('60', '100'); //outputs 5. The last example is confusing me. I don't understand why it is outputting a positive number. ASCII

Fastest way of comparing strings in C

自古美人都是妖i 提交于 2019-12-04 05:16:12
问题 I wanted to know if there are even faster ways of comparing strings in C than using strcmp() , especially when I have to compare a string with multiple pre-defined strings in a switch statement fashion. In my application, the string to be compared can sometimes go as big as 1000 chars, so was just thinking if strcmp() is sufficient enough or if there exists better and efficient way which I am not familiar with. I am actually working on a low power embedded IoT project where more CPU cycles

Templated Functions.. ERROR: template-id does not match any template declaration

我与影子孤独终老i 提交于 2019-12-04 03:38:28
问题 I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works fine. But I want to work with char* type . This is the error I get=> error: template-id ‘Max<>’ for ‘void Max(char, char, char)’ does not match any template declaration Following is my code: template <typename T> void Max(T& a,T& b,T& c) { if(a > b && a

What does strcmp return if two similar strings are of different lengths?

淺唱寂寞╮ 提交于 2019-12-04 03:11:50
I understand that if you have 'cat' (string1) and 'dog' (string2) in strcmp (this is a C question) then the return value of strcmp would be less than 0 (since 'cat' is lexically less than 'dog'). However, I am not sure what would happen with strcmp if this happened: string1: 'dog' string2: 'dog2'. What would strcmp return? Less than zero, zero, or greater than? For context, I am trying to write a comparator function that compares strings and would like to account for strings starting with the same characters. One string may have an extension (such as '2' in 'dog2' in the example above). EDIT:

Optimized strcmp implementation

冷暖自知 提交于 2019-12-03 12:30:41
This function was found here . It's an implementation of strcmp : int strcmp(const char* s1, const char* s2) { while (*s1 && (*s1 == *s2)) s1++, s2++; return *(const unsigned char*)s1 - *(const unsigned char*)s2; } I understand all but the last line, in short what is going on in the last line? chux - Reinstate Monica return *(const unsigned char*)s1-*(const unsigned char*)s2; OP: in short what is going on in the last line? A: The first potential string difference is compared. Both chars are referenced as unsigned char as required by the spec. The 2 are promoted to int and the difference is

How to compare two strings case and diacritic-insensitive?

我的梦境 提交于 2019-12-03 08:17:50
I have two strings String 1: "sebastien" String 2: "Sébastien" I want to compare these two strings by ignoring é (Accents) character. Can anyone know this logic? Thanks in advance <?php $string_1 = 'sebastien'; $string_2 = 'Sébastien'; function transliterateString($txt) { $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' =