To compare two string in C Programming, you have to ask to the user to enter the two string and start comparing using the function strcmp. If it will return 0, then both string will be equal and if it will not return 0, then both string will not be equal to each other as shown in here in the following program.
This came up in another thread. I gave some advice that I'm not longer sure of. Rather than hijacking that thread, I figured I'd start my own. I advised against using the in that thread. The context was this:
- The strcmp function in C compares two null terminating string. The comparison is done lexicographically.
- The strcmp compares two strings character by character. If the first character of two strings is equal, the next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '0' is reached. It is defined in the string.h header file.
- C assignments, questions and answers on C-style Strings. Finding lenght of string, compare two strings, concatenate two strings, checking for palindrom, reversing a string, converting a given string to uppercase or lowercase are discussed in this assignment.
- 'dev' is not a string it is a const char. like var1. Thus you are indeed comparing the memory adresses. Being that var1 is a char pointer,.var1 is a single char (the first character of the pointed to character sequence to be precise). You can't compare a char against a char pointer, which is why that did not work.
- C provides following two types of string representations − The C-style character string. The string class type introduced with Standard C. The C-Style Character String. The C-style character string originated within the C language and continues to be supported within C.
Ancient Dragon said the above was fine and that compare was unnecessary. So I wondered if maybe was only bad when comparing two string variables, so I did a little test program and it worked:
The line displayed to the screen. So my question is when is it bad to use when comparing strings? I thought the program above was not supposed to work. Apparently I was mistaken. If it's always fine to use , do we ever need to use the 'compare' function?
- 4 Contributors
- forum 11 Replies
- 760 Views
- 2 Days Discussion Span
- commentLatest Postby VernonDozierLatest Post
![C++ string compare fast C++ string compare fast](/uploads/1/2/6/3/126304215/731927990.png)
Recommended Answers
Heres the same basic information using a slightly different explanation:
Given:
string a = 'hello';
string a = 'hello';
a is not an address. The STL string object may have within it a char (to be used as a C style string) as a member variable, in addition to other member variables such …
Jump to PostC++ Compare String To Char
All 11 Replies
Ancient Dragon5,243
![Dev Dev](/uploads/1/2/6/3/126304215/383646486.png)
>>string subChoice =';
It isn't necessary to provide an initializer for strings because that's the default. Just
It isn't necessary to provide an initializer for strings because that's the default. Just
string subChoice;
is sufficient.C++ String Compare Fast
>> do we ever need to use the 'compare' function
You use it when you need to know if one string is less than, equal to, or greater than the second string, such as in sorting algorithms. You could also use the < and > operators but then that might be too slow when used in if conditions because the comparison would have to be repeated.
You use it when you need to know if one string is less than, equal to, or greater than the second string, such as in sorting algorithms. You could also use the < and > operators but then that might be too slow when used in if conditions because the comparison would have to be repeated.