strcmp

Inconsistent strcmp() return value when passing strings as pointers or as literals

倾然丶 夕夏残阳落幕 提交于 2019-11-27 21:51:17
I was playing around with strcmp when I noticed this, here is the code: #include <string.h> #include <stdio.h> int main(){ //passing strings directly printf("%d\n", strcmp("ahmad", "fatema")); //passing strings as pointers char *a= "ahmad"; char *b= "fatema"; printf("%d\n",strcmp(a,b)); return 0; } the output is: -1 -5 shouldn't strcmp work the same? Why is it that I am given different value when I pass strings as "ahmad" or as char* a = "ahmad" . When you pass values to a function they are allocated in its stack right? Shafik Yaghmour You are most likely seeing the result of a compiler

C Primer Plus 第11章 字符串和字符串函数 11.5 字符串函数

♀尐吖头ヾ 提交于 2019-11-27 19:30:04
C库提供了许多处理字符串的函数:ANSI C 用头文件string.h给出这些函数的原型。下面是一些最有用和最常用的函数:strlen() 、strcat()、strncat() 、strcmp() 、strncmp() 、strcpy()、 strncpy()。此外,我们也将研究一下头文件stdio.h支持的sprintf()函数。 11.5.1 strlen( )函数 我们已经知道, 用strlen()函数可以得到字符串的长度。 下面的函数中用到了strlen()函数,这是一个可以缩短字符串长度的函数: /*test_fit.c-*/ void fit (char * string,unsigned int size) { if(strlen(string)>size) *(string+size)='\0'; } 这个函数确实要改变字符串,因此在函数头中声明形式参量string时没有使用const修饰符。 在程序清单11.13的程序中测试一下fit( )函数。注意,代码中用到C的字符串文本串联功能。 程序清单11.13 test.c程序 /*test.c 试用缩短字符串的函数*/ #include <stdio.h> #include <string.h> void fit(char *,unsigned int ); int main(void) { char msg[]=

How to properly compare command-line arguments?

浪子不回头ぞ 提交于 2019-11-27 16:10:35
I am trying to write a C code which takes arguments in main; thus when I write some strings in cmd, the program doing somethings inside it. But I am doing something wrong and I can't find it. This is the code: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]){ //File name is main.c if(argc != 3) printf("Wrong!!!!!!!!!"); else if (argv[1] == "-s") girls(); //Prints "Girls" else if(argv[1] == "-k") boys(); //Prints "Boys" else printf("OMG!!"); } In the cmd; gcc -o gender main.c gender -s pilkington I enter that commands. Bu the output is always "OMG!!" Which part is wrong?

字符串处理3-比较

天大地大妈咪最大 提交于 2019-11-27 10:17:27
字符串的比较   到目前为止,我们已经用过“==”号来比较两个字符串是否相等,使用PHP可以进行一些更复杂的比较,这些分为两类:部分匹配和其他情况   字符串的排序:strcmp()、strcasemp()strnatcmp()   strcmp()该函数需要两个进行比较的参数字符串。如果两个字符串相等,该函数返回0,如果按字典顺序str1和str2后面(大于str2)就返回一个整数,如果str1小于str2就返回一个负数,这个函数是区分大小写的。   函数strcasecmp()除了区分大小写之外,其他和strcmp()一样 来源: https://www.cnblogs.com/xiaowie/p/11358742.html

What does strcmp() exactly return in C?

若如初见. 提交于 2019-11-27 09:49:09
I wrote this code in C: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> int main() { char string1[20]; char string2[20]; strcpy(string1, "Heloooo"); strcpy(string2, "Helloo"); printf("%d", strcmp(string1, string2)); return(0); } Should console print value 1 or difference between ASCII values of o and \0 character i.e. 111? On this website , it is written that this should give out put 111, but when I run it on my laptop, it shows 1. Why? From the cppreference.com documentation int strcmp( const char *lhs, const char *rhs ); Return value Negative value if lhs appears

When will strcmp not return -1, 0 or 1?

我的未来我决定 提交于 2019-11-27 08:15:09
问题 From the man page: The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. Example code in C (prints -15 on my machine, swapping test1 and test2 inverts the value): #include <stdio.h> #include <string.h> int main() { char* test1 = "hello"; char* test2 = "world"; printf("%d\n", strcmp(test1, test2)); } I found this code (taken from this

C++中常用的字符串函数

风流意气都作罢 提交于 2019-11-27 07:42:45
C++语言提供了比C语言更丰富的字符串处理功能。它可以在字符串上经行输入,输出,合并,修改,比较,转换,复制,搜索等操作。使用这些现成的功能可以大大减少我们的编程的负担。 输入和输出的字符串函数,如printf,puts,cout,scanf,gets,cout等,在使用时应包含头文件cstdio,并使用其他字符串函数包含头文件cstring。 cstring是一个专门用于处理字符串的头文件。它包含许多字符串处理函数。由于篇幅限制,本节只能解释一些常见的内容。 字符串连接函数 strcat() strcat 就是 string catenate 的缩写,意思为把两个字符串拼在一起,其格式为: strcat(Str1, Str2); Str1、Str2 为需要拼接的字符串。 strcat() 将把 Str2 连接到 Str1 后面,并删除原来 Str1 最后的结束标志\0。这意味着,Str1 必须足够长,要能够同时容纳 Str1 和 Str2,否则字符数组会越界(超出字符串范围)。 strcat() 的返回值为 Str1 的地址。 这是一个简单的演示: #include <cstdio> #include <cstring> int main(){ char str1[100]="The URL is "; char str2[60]; cout<<"Input a URL: ";

lvalue required as left operand of assignment

断了今生、忘了曾经 提交于 2019-11-27 05:44:31
问题 Why am I getting lvalue required as left operand of assignment with a single string comparison? How can I fix this in C ? if (strcmp("hello", "hello") = 0) Thanks! 回答1: You need to compare, not assign: if (strcmp("hello", "hello") == 0) ^ Because you want to check if the result of strcmp("hello", "hello") equals to 0 . About the error: lvalue required as left operand of assignment lvalue means an assignable value (variable), and in assignment the left value to the = has to be lvalue (pretty

Implementation of strcmp

情到浓时终转凉″ 提交于 2019-11-27 01:57:43
问题 I tried to implement strcmp: int strCmp(char string1[], char string2[] ) { int i=0,flag=0; while(flag==0) { if (string1[i]>string2[i]) { flag=1; } else if (string1[i]<string2[i]) { flag=-1; } else { i++; } } return flag; } but I'm stuck with the case that the user will input the same strings, because the function works with 1 and -1, but it's doesn't return 0. Can anyone help? And please without pointers! 回答1: You seem to want to avoid pointer arithmetics which is a pity since that makes the

strcmp() return values in C [duplicate]

こ雲淡風輕ζ 提交于 2019-11-27 01:49:35
This question already has an answer here: How does strcmp() work? 9 answers I am learning about strcmp() in C. I understand that when two strings are equal, strcmp returns 0. However, when the man pages state that strcmp returns less than 0 when the first string is less than the second string, is it referring to length, ASCII values, or something else? In this sense, "less than" for strings means lexicographic (alphabetical) order. So cat is less than dog because cat is alphabetically before dog . Lexicographic order is, in some sense, an extension of alphabetical order to all ASCII (and