strcmp

strcmp() return values in C [duplicate]

前提是你 提交于 2019-12-17 06:46:12
问题 This question already has answers here : How does strcmp() work? (9 answers) Closed 2 years ago . 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? 回答1: In this sense, "less than" for strings means lexicographic (alphabetical) order. So cat is less than dog because

2019-12-15

纵然是瞬间 提交于 2019-12-16 06:14:37
strcmp()字符串比较函数用法 函数原型: int strcmp(const char *s1, const char *s2); 头文件: #include <string.h> 功能: 用来比较两个字符串 参数: s1、s2为两个进行比较的字符串 返回值: 若s1、s2字符串相等,则返回零;若s1大于s2,则返回大于零的数;否则,则返回小于零的数。 说明: strcmp()函数是根据ACSII码的值来比较两个字符串的;strcmp()函数首先将s1字符串的第一个字符值减去s2第一个字符,若差值为零则继续比较下去;若差值不为零,则返回差值。 直到出现不同的字符或遇’\0’为止。 特别注意: strcmp(const char * s1,const char * s2) 这里面只能比较字符串,不能比较数字等其他形式的参数。 代码示例: #include <string.h> int main(void) { char *p=“aBc”; char *q=“Abc”; char *h=“abc”; printf(“strcmp(p,q):%d\n”,strcmp(p,q)); printf(“strcmp(p,h):%d\n”,strcmp(p,h)); return 0; 1 2 3 4 } //结果: //strcmp(p,q):32 //strcmp(p,h):-32L。

Why is my strcmp() failing?

北战南征 提交于 2019-12-13 05:25:55
问题 I am a C newbie and learning string tokenizing. I am trying to compare two strings in the following way. But the string comparison I am doing is failing. Can you please let me know what I am missing here? I couldn't find another similar question, may be due to my inexperience in C. If one exists, can you please redirect me to it? char* input = "comparer here"; char* args[5]; int counter = 0; char *tok = strtok(input, " "); while (tok != NULL) { args[counter] = tok; counter ++; if (counter ==

How to compare a string with symbol '\'

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 02:59:46
问题 I have a project and somewhere in my code I'm writing this: else if (character[0] == '\'){ How can I compare my character with this symbol? Every other symbol that I tried to compare like , , ; , etc. is done and this is the only symbol that I'm getting a wrong message. 回答1: The backslash \ is used as an escape character, so you would need to write: else if (character[0] == '\\'){ In this case the backslash is being used to escape itself. 回答2: I'm not clear on what you ask. Do you mean to

Comparing two strings, problems with strcmp

浪尽此生 提交于 2019-12-12 15:53:21
问题 I'm trying to check if the line read from stdin begins with "login:" but strcmp does not seem to work. char s1[20], s2[20]; fgets(s1, 20, stdin); strncpy(s2,s1,6); strcmp(s2, "login:"); if( strcmp(s2, "login:") == 0) printf("s2 = \"login:\"\n"); else printf("s2 != \"login:\"\n"); I don't care what comes after "login:", i just want to make sure that's how the command is given. What am i doing wrong? 回答1: strcmp returns 0 if the two strings are exactly the same to accomplish what you want to do

Asking a Yes or No query in MATLAB

笑着哭i 提交于 2019-12-12 09:17:18
问题 I have written a script in MATLAB, where I am retrieving rows and columns from a table based on the WHERE clause. So far i manage to retrieve the data from the database table. The problem is that i would like to allow the user to have the option of running another search to retrieve another set of data. This is my code so far that i have and the script is called 'searchpdb'. pdbSearch = input('Enter your PDB Code: ', 's') curs = fetch(exec(conn, ['SELECT * FROM cath_2_wo_dup WHERE pdbcode = '

Passing an array of strings to a function-segmentation fault

一曲冷凌霜 提交于 2019-12-12 06:20:33
问题 So, I've written this code below that is supposed to pass an array of strings to a function, which then sorts the array into alphabetical order. I know the way I've done it probably isn't pretty, but it is for school and I'm required to pass it to a function and make use of strcmp . I ran into some problems, but I managed to get all the compile errors sorted. Now, however, when I try to run the program, I get the error segmentation fault(core dumped) . Can someone guide me to where I made my

How to check if a string is in a file

偶尔善良 提交于 2019-12-12 01:34:09
问题 I'm new to C and I am trying to figure out how I can search for a specific string and the number that follows it in a file that has several numbers and words. The input file that I'm using looks like this: TREES 2 BENCHES 5 ROCKS 10 PLANTS 8 I know how to make a function read a string and I know how to compare two strings but I don't know how to put them both together or how to set the array up to read through the whole file. I've been using strcmp , but I just don't know how to go about

PHP strcmp is not returning the correct answer with session variable

好久不见. 提交于 2019-12-11 23:36:30
问题 I have a session variable $_SESSION['condition'], I know it is set because when I entered this: echo $_SESSION['condition']." = "."Below Average"; It returns: Below Average = Below Average When I do a gettype() on the session variable it returns type "string". Yet when I do strcmp() it returns: -34 I have also tried an IF statement with == rather than strcmp testing for equality AND an IF statement casting them both as strings and testing if they are equal with no luck. Any reason why this

Typecast for qsort function pointer

强颜欢笑 提交于 2019-12-11 13:37:05
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to char", but strcmp(3) arguments are "pointers to char", hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const *) p2); } int main(int argc, char *argv[]) { int j; assert(argc > 1); qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp); for (j = 1; j