strcmp

Why use strcmp instead of == in C++?

风格不统一 提交于 2021-02-18 21:01:26
问题 I wonder my code works perfectly fine either using strcmp or simply == in C++ for comparing 2 char arrays. Can any one justify the reason of using strcmp instead of == ; 回答1: strcmp compares the actual C-string content, while using == between two C-string is asking if these two char pointers point to the same position. If we have some C-string defined as following: char string_a[] = "foo"; char string_b[] = "foo"; char * string_c = string_a; strcmp(string_a, string_b) == 0 would return true ,

Subtracting two characters

我的未来我决定 提交于 2021-02-16 18:13:12
问题 I just started programming in assembly so I am a beginner. To practice, I am trying to rewrite a basic libc in assembly (NASM Intel syntax). But I'm stuck on the strcmp function: ;; Compare two C-style NUL-terminated strings ;; Inputs : ESI = address of s1, EDI = address of s2 ;; Outputs : EAX = return an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2 strcmp: call strlen mov ecx, eax ; ecx = length of the string

Efficient string sorting algorithm

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-06 15:51:16
问题 Sorting strings by comparisons (e.g. standard QuickSort + strcmp-like function) may be a bit slow, especially for long strings sharing a common prefix (the comparison function takes O(s) time, where s is the length of string), thus a standard solution has the complexity of O(s * nlog n). Are there any known faster algorithms? 回答1: If you know that the string consist only of certain characters (which is almost always the case), you can use a variant of BucketSort or RadixSort. 回答2: You could

Efficient string sorting algorithm

谁说我不能喝 提交于 2021-02-06 15:50:07
问题 Sorting strings by comparisons (e.g. standard QuickSort + strcmp-like function) may be a bit slow, especially for long strings sharing a common prefix (the comparison function takes O(s) time, where s is the length of string), thus a standard solution has the complexity of O(s * nlog n). Are there any known faster algorithms? 回答1: If you know that the string consist only of certain characters (which is almost always the case), you can use a variant of BucketSort or RadixSort. 回答2: You could

Why strcmp function is not comparing the received command from the user with LIST ( without strcmp the function is working )

旧街凉风 提交于 2021-01-29 19:42:41
问题 I am new to socket programming, I am writing an FTP server without the client I have to access the server using netcat localhost port void do_job(int fd) { i,client; char command[DEFAULT_BUFLEN]; while((client =recv(fd, command, strlen(command), 0)) >0 ) { if (strcmp(command,"LIST") ==0) { } in the main function : if ((pid=fork()) == 0) { close(listenfd); do_job(fd); printf("Child finished their job!\n"); close(fd); exit(0); } 回答1: You need to add a null terminator to the string in order to

basename on buffer goes into segmentation fault

时间秒杀一切 提交于 2021-01-29 04:26:43
问题 I'm tweaking with basename right now and I encounter a case really weird (at least for me). Here's the code: char buffer[300]; char* p; strcpy(buffer, "../src/test/resources/constraints_0020_000"); printf("%d\n", strcmp(basename("../src/test/resources/constraints_0020_000"), "constraints_0020_000")); //works as expected printf("assert testBasename02"); printf("%d\n", strcmp(basename(buffer), "constraints_0020_000") == 0); printf("done 1\n"); //goes in segmentation fault printf("%d\n", strcmp

GDB - strcmp not working: __strcmp_sse2_unaligned

痞子三分冷 提交于 2021-01-27 10:48:01
问题 I'm unable to create conditional breakpoint in GDB using strcmp: break x if strcmp(str.c_str(), "foo") == 0 Why you ask? Because: print strcmp("hello", "hello") Yield's (int (*)(const char *, const char *)) 0x7ffff76ffe70 <__strcmp_sse2_unaligned> Even when casting it to an integer: print (int)strcmp("hello", "hello") It returns some nonsensical value like -143655312 Here's a less graceful way to "solve" my problem. I can define a function in my own code: int mystrcmp(const char *str1, const

GDB - strcmp not working: __strcmp_sse2_unaligned

本秂侑毒 提交于 2021-01-27 10:46:25
问题 I'm unable to create conditional breakpoint in GDB using strcmp: break x if strcmp(str.c_str(), "foo") == 0 Why you ask? Because: print strcmp("hello", "hello") Yield's (int (*)(const char *, const char *)) 0x7ffff76ffe70 <__strcmp_sse2_unaligned> Even when casting it to an integer: print (int)strcmp("hello", "hello") It returns some nonsensical value like -143655312 Here's a less graceful way to "solve" my problem. I can define a function in my own code: int mystrcmp(const char *str1, const

Can strcmp() work with strings in c++?

痴心易碎 提交于 2020-07-03 08:15:41
问题 I have this line of code if(strcmp(ob[i].getBrand(), ob[j].getBrand()) > 0) and I get this error error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' Does that mean that strcmp doesnt work with strings, but instead it has to convert it to char? 回答1: Don't use strcmp . Use std::string::compare which has the same behavior as strcmp . if(ob[i].getBrand().compare(ob[j].getBrand()) > 0) Or much better if(ob[i].getBrand() > ob[j].getBrand()) Generally you should

Can strcmp() work with strings in c++?

懵懂的女人 提交于 2020-07-03 08:14:32
问题 I have this line of code if(strcmp(ob[i].getBrand(), ob[j].getBrand()) > 0) and I get this error error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' Does that mean that strcmp doesnt work with strings, but instead it has to convert it to char? 回答1: Don't use strcmp . Use std::string::compare which has the same behavior as strcmp . if(ob[i].getBrand().compare(ob[j].getBrand()) > 0) Or much better if(ob[i].getBrand() > ob[j].getBrand()) Generally you should