Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have slice of C code and I want to ask when I input some string for example "up".
Jump to the code looks like : (strcmp(pdirection,"up")==0)
So, what does it mean, I don't understand especially ==0
the slice of code is at call by reference position.
From the manual
int strcmp(const char *s1, const char *s2);
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.
So, if both the strings are equal, then strcmp() returns 0
The == is the equal to operator, which checks if the value on both sides are equal. Let's see what the C standard has to say
6.5.9
equality-expression == relational-expression
3 The == (equal to) and != (not equal to) operators are analogous to
the relational operators except for their lower precedence.108) Each
of the operators yields 1 if the specified relation is true and 0 if
it is false. The result has type int. For any pair of operands,
exactly one of the relations is true.
Your code was probably
if( strcmp(pdirection,"up") == 0 )
do_something;
So, if the string stored in pdirection is equal to "up", then the function strcmp() will return 0 and the == 0 part checks if the value is equal to 0, and if it is equal to 0, then do_something is done.
The strcmp(char *str1, char *str2) return the difference between the 2 strings. So if the 2 strings are equal, the difference is 0.
In fact, it scans the 2 strings one char at a time, until it reaches 2 different chars or passes the end of one string (the last char of a string in C is a char whose ASCII value is 0, noted '\0').
The function returns the first difference it meets between the 2 strings.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
for example
string1='abbbc'
string2='agdee'
they have the same character 'a'
so i have to return 1;
else if like
string1'abbb'
string2'cccd'
they don't have any same character
so i have to return 0;
how to boost this searching process?
how to do this rather than using double for loop in C?
As an alternative algorithm:
Have an array of bool, size big enough to hold the highest character (e.g. ascii z is 122) you will have
Loop over the first string converting each char to its numerical and set the bool in that array index to true
Loop over the second string changing each char to numeric and using it to read the bool in that array position
If you encounter a true, return 1
if you reach the end of the array with no trues encountered return 0
This essentially converts the "wase cpu time" of checking every char against every other char (two strings of length 5, 25 comparisons) into a "burn some memory" one (two strings of length 5, up to 10 comparisons, at the expense of holding maybe 122 bools worth of memory), so there's a trade off point that varies with the length of the strings and the size of the charset.. eg two strings of length 5 that could be any Unicode.. it ain't worth burning 65k bools of memory just to save 15 ish comparisons
This question already has answers here:
Is this the only return value for strcmp() in C?
(4 answers)
strcmp behaviour in 32-bit and 64-bit systems
(3 answers)
Closed 2 years ago.
So I studied that strcmp returns the difference between the asci value of the two characters being compared. But in my case it is giving a value of -1,0 or 1 only.
#include<stdio.h>
#include<strings.h>
int main()
{
char n1[]="Jerry";
char n2[]="Ferry";
printf("%d",strcmp(n2,n1));
return 0;
}
Ideally it should give -4,but dev cpp is giving an output of -1. Why is that?
strcmp gives the relative difference between 2 strings.
negative if the left item is less than right
0 if the left is the same as the right
positive if the left item is greater than the right
It doesn't give a range on the difference.
(HINT: what value for strcmp( "Hello", "Hfllo") ? )
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have searched for any reference to this for quite a bit now but I haven't had any success, so I thought I would ask here. Basically, I am trying to understand a C written program for creating a shell in linux and I am having problems with this piece of code
...
else if (args[0][0]-'!'==0){
int x = args[0][1]-'0';
int z = args[0][2]-'0';
...
}
The args is storing the command's entered by the user. For instance, later the address space of the child (parent process reads the commands, child executes them) is replaced using a call to execvp(args[0], args). The definition of args is as follows: char *args[MAX_LINE/2 +1];
What I have been having trouble understanding is the ways in which the array is accessed; specifically what is meant by these expressions in this context:
args[0][0]-'!'==0
args[0][1]-'0';
args[0][2]-'0';
Judging by the name of the variable, args stands for a list/array of arguments.
arg[0] is the first element of that array.
args[0][0] is the first character of the first element of that array.
The expression args[0][0]-'!'==0 checks whether that character is equal to '!'. That could have been written better as args[0][0] == '!'.
It's as if instead of using if ( i == 10 ), you decide to use if (i-10 == 0).
The next two lines
int x = args[0][1]-'0';
int z = args[0][2]-'0';
expect that the second and third characters of the first argument are digits and extract the decimal values they correspond to. If the first argument is "!26", then x will have the value 2 and z will have the value 6.
That logic depends on the guarantee that the encoding used for the characters '0' - '9' are required to be contiguous.
Probably args is a reference to
int main(int argc, char **argv);
Then args[0] is the name of the program and in the following args you would find the Arguments of the Programm, see e.g. Arguments to main in C
Thus args[0[0] is the first character of the name of the program.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Can anyone explain what is the meaning of this line of code
printf("%d",strcmp("strcmp()","strcmp()"))
The code is just a joke...
It just compares two fixed strings. To make it confusing the two strings contain the name of a library function (strcmp) but that - of cause - doesn't make it a function call.
The code
printf("%d",strcmp("strcmp()","strcmp()"))
is similar to
printf("%d",strcmp("A","A"))
It will print 0 (zero) as the strings are identical and strcmp return zero when strings have an exact compare.
strcmp() is responsible for lexicographic comparison of strings(character order).i.e.first character compared with first character if equal then second and so on.
Its signature is as follows:
int strcmp(const char *str1, const char *str2)
http://forgetcode.com/C/1026-strcmp-lexicographically-compares-two-strings
https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm
This function return values that are as follows:
if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am working on strings in C and I would like to ask What exactly Lexicographic Order is and how is being used in C. Which is the best way to compare 2 strings . I have read about strcmp and it's lexicographic comparison but I am confused.
strcmp uses lexicographic order to compare strings. That is, it follows the alphabet. In English, F comes directly before G, and Z comes directly after Y. strcmp takes this into account, because the characters in the ASCII table succeed in alphabetical order. A typical strcmp function would be
int strcmp(const char *a, const char *b)
{
for (; *a && *b && *a == *b; ++a, ++b)
;
return *b - *a;
}
It loops through the characters, while the \0 was not seen, and the current characters are equal. As soon as the characters become unequal or the \0 is seen, the loop is broken, and the return expression is evaluated. If the \0 was seen, then both characters are equal, and \0 - \0 must be 0.
Lexicographic order is the order defined by the lexicon, or the order defined by how people communicate. We English-speaking humans agree that in our lexicon the word Apple comes before the word Cat, we call it 'alphabetical order'. In your case, using strcmp, it will be the order defined by the ASCII chart, which also works out to be alphabetical as long as you use a consistent case. An 'A' is less than a 'B' because the 'A' appears before the 'B' in the ASCII chart.