strcmp

strcasecmp in C returns 156 instead of 0, any ideas why?

一笑奈何 提交于 2019-12-11 03:04:40
问题 I have the following code: printf("num: %d\n", strcasecmp(buf, "h\n")); And I get the following results when I try plugging in different letters: a: -7 g: -1 i: 1 j: 2 h: 156 H: 156 Should strcasecmp not return 0 when buf is equal to H or h ? Any ideas why it's returning 156? I need to figure out how to check whether the user types H or h . Thanks! Edit: I'm reading buf in the following way: read(0, buf, MAXBUFLEN); 回答1: printf("num: %d\n", strcasecmp(buf, "h")); Why \n at the end, if you

游戏吧,石头剪刀布

风流意气都作罢 提交于 2019-12-10 22:05:52
# include <iostream> # include <string.h> char a [ 10 ] [ 10 ] ; using namespace std ; int main ( ) { int N ; cin >> N ; while ( N -- ) { for ( int i = 0 ; i < 2 ; i ++ ) cin >> a [ i ] ; if ( strcmp ( a [ 0 ] , a [ 1 ] ) == 0 ) cout << "Tie" << endl ; else { if ( strcmp ( a [ 0 ] , "Rock" ) == 0 && strcmp ( a [ 1 ] , "Paper" ) == 0 ) cout << "Win" << endl ; else if ( strcmp ( a [ 0 ] , "Paper" ) == 0 && strcmp ( a [ 1 ] , "Scissors" ) == 0 ) cout << "Win" << endl ; else if ( strcmp ( a [ 0 ] , "Scissors" ) == 0 && strcmp ( a [ 1 ] , "Rock" ) == 0 ) cout << "Win" << endl ; else cout << "Fail"

strcmp function not working properly

半城伤御伤魂 提交于 2019-12-10 21:14:45
问题 I have a delete function on array of structures books . I'm passing it an array of records, author of book and name of book and size of the list . Now here given that list[0].author and list[5].author and author all are equal to "Dan Brown" (same string) void delete(struct books *list,char author[],char name[],int n) { int i,a; a=strcmp(list[0].author,list[5].author); printf("%d\n",a); // prints 0 a=strcmp(list[0].author,author); printf("%d\n",a); // prints other than 0 } Why is it happening?

Why the returns of strcmp is different? [duplicate]

可紊 提交于 2019-12-10 21:13:14
问题 This question already has answers here : Inconsistent strcmp() return value when passing strings as pointers or as literals (2 answers) Closed 4 years ago . Here is the C code and I compiled with gcc char *a="a"; char *d="d"; printf("%d\n", strcmp("a", "d")); printf("%d\n", strcmp(a, "d")); printf("%d\n", strcmp(a, d)); When I compiled with -O the output is -1 -3 -1 When I compiled without -O then output is -1 -3 -3 Why the output is different and what is the code of strcmp ? 回答1: Why the

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

Deadly 提交于 2019-12-09 16:20:11
问题 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

C 简陋版string操作strcpy strcmp strcat strchr strstr

空扰寡人 提交于 2019-12-09 11:39:02
文件下载地址: http://pan.baidu.com/s/1bn2BcTL 代码如下: #include <stdio.h> #include <stdlib.h> char *strcpy_(char *dest,const char *src); char *strcat_(char *dest,const char *src); int strcmp_(const char *dest,const char *src); int strlen_(const char *src); char *strchr_(char *s, int c); char *strstr_(const char *s,const char *c); int main() { char p[]="xxdexxx"; char q[]="de"; printf("p=%s\n",p); printf("q=%s\n",q); printf("strlen_(p)=%d\n",strlen_(p)); printf("strcpy_(p,q)=%s\n", strcpy_(p,q)); char p1[]="xxdexxx"; char q1[]="de"; printf("strchr_(p,'d')=%s\n",strchr_(p1,'d')); char p2[]="xxdexxx";

c语言常用函数strcmp函数和strcpy函数

跟風遠走 提交于 2019-12-09 11:37:48
(一)strcmp函数 strcmp函数是比较两个字符串的大小,返回比较的结果。一般形式是: i=strcmp(字符串,字符串); 其中,字符串1、字符串2均可为字符串常量或变量;i 是用于存放比较结果的整型变量。比较结果是这样规定的: ①字符串1小于字符串2,strcmp函数返回一个负值; ②字符串1等于字符串2,strcmp函数返回零; ③字符串1大于字符串2,strcmp函数返回一个正值;那么,字符中的大小是如何比较的呢?来看一个例子。 实际上,字符串的比较是比较字符串中各对字符的ASCII码。首先比较两个串的第一个字符,若不相等,则停止比较并得出大于或小于的结果;如果相等就接着 比较第二个字符然后第三个字符等等。如果两上字符串前面的字符一直相等,像"disk"和"disks" 那样, 前四个字符都一样, 然后比较第 五个字符, 前一个字符串"disk"只剩下结束符'/0',后一个字符串"disks"剩下's','/0'的ASCII码小于's'的ASCII 码,所以得出了结果。因此无论两个字符串是什么样,strcmp函数最多比较到其中一个字符串遇到结束符'/0'为止,就能得出结果。 注意:字符串是数组类型而非简单类型,不能用关系运算进行大小比较。 if("ABC">"DEF") /*错误的字符串比较*/ if(strcmp("ABC","DEF") /*正确的字符串比较*/

【面试题】C语言:模拟实现memcmp,试比较memcmp与strcmp,strncmp的区别

筅森魡賤 提交于 2019-12-08 17:22:17
模拟实现内存比较函数memcmp: 该函数与strcmp有相似之处, 都可用于字符串比较是否相同 ,若相同,则返回0值。若前者大于后者,则返回大于0的整型值,否则返回小于0的整型值。 区别在于: strcmp只能比较字符串,memcmp是内存比较函数,原则上是比较内存的,但其实真正实现时并不是所有都可以比较,例如float,但我们至少可以比较字符串以及int型。 而对于strcmp,strncmp的比较:str1, str2 为需要比较的两个字符串,n为要比较的字符的数目,而函数strcmp()做不到,strcmp()可以比较全部字符串(因为它找字符串结束标志‘\0’)。 关于strcmp的实现,可以查看我的博客 http://10740184.blog.51cto.com/10730184/1714512 关于strncmp的实现,可以查看我的博客 http://10740184.blog.51cto.com/10730184/1715207 代码如下: #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<assert.h> int my_memcmp(const void *p1, const void *p2, size_t count) { assert(p1);

strcmpi renamed to _strcmpi?

China☆狼群 提交于 2019-12-08 16:25:43
问题 In MSVC++, there's a function strcmpi for case-insensitive C-string comparisons. When you try and use it, it goes, This POSIX function is deprecated beginning in Visual C++ 2005. Use the ISO C++ conformant _stricmp instead. What I don't see is why does ISO not want MSVC++ to use strcmpi, and why is _stricmp the preferred way, and why would they bother to rename the function, and how is a function beginning with an underscore ISO conformant. I know there must be a reason for all this, and I'm

NTP协议实现

匆匆过客 提交于 2019-12-07 21:30:16
10.4 实验内容 ——NTP协议实现 1.实验目的 通过实现NTP协议的练习,进一步掌握Linux网络编程,并且提高协议的分析与实现能力,为参与完成综合性项目打下良好的基础。 2.实验内容 Network Time Protocol(NTP)协议是用来使计算机时间同步化 的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等)做同步化,它可以提供高精确度的时间校正(LAN上与标准时间差小于1毫秒,WAN上几十毫秒),且可用加密确认的 方式来防止协议攻击。 NTP提供准确时间,首先要有准确的时间来源,这一时间应该是国际标准时间UTC。 NTP获得UTC的时间来源可以是原子钟、天文台、卫星,也可以从Internet上获取。这样就有了准确而可靠的时间源。时间是按NTP服务器的等级传 播。按照距离外部UTC 源的远近将所有服务器归入不同的Stratun(层)中。Stratum-1在顶层,有外部UTC接入,而Stratum-2则从Stratum-1获取 时间,Stratum-3从Stratum-2获取时间,以此类推,但Stratum层的总数限制在15以内。所有这些服务器在逻辑上形成阶梯式的架构并 相互连接,而Stratum-1的时间服务器是整个系统的基础。 进行网络协议实现时最重要的是了解协议数据格式。NTP数据包有48个字节,其中NTP包头16字节,时间戳32个字节