String ในภาษา c/c ในภาษา c/c ตัวแปร String คือการนําตัวแปรชนิด char หลายๆตัวมาต่อกัน หรืออาจจะเรียกว่า char array โดยจะบอกจุดสิ้นสุดของ String ด้วยตัวอักษร(char) '0'.
C++ How To Compare Strings
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:
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
- 751 Views
- 2 Days Discussion Span
- commentLatest Postby VernonDozierLatest Post
Recommended Answers
Heres the same basic information using a slightly different explanation:
Apr 29, 2010 Posted: Thu Apr 29, 2010 11:15 am Post subject: If using strings in dev-c I am using latest version of dev-c and i am trying to do an if with strings. So here's my code, i. String ในภาษา c/c ในภาษา c/c ตัวแปร String คือการนําตัวแปรชนิด char หลายๆตัวมาต่อกัน หรืออาจจะเรียกว่า char array โดยจะบอกจุดสิ้นสุดของ String ด้วยตัวอักษร(char) ' 0'. C String Library - compare - It compares the value of the string object (or a substring) to the sequence of characters specified by its arguments. Compares the value of the string object (or a substring) to the sequence of characters specified by its arguments. The compared string is the value of the string object or -if the signature used has a pos and a len parameters- the substring that begins at its character in position pos and spans len characters.
Given:
string a = 'hello';
string a = 'hello';
![String compare dev c 4 String compare dev c 4](/uploads/1/2/6/1/126130250/380680526.png)
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 Post
All 11 Replies
Ancient Dragon5,243
>>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.
>> 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.
Hello! :)
I'm having a problem while comparing one character from a string with a ... string.
This is a part of a pretty complex loop (or, it's complex for me, since I'm new to C++ and programming), but the case is that I need to compare the value of a given i index of a string to a string. For example:
Well, this is just a silly example program, but I hope you see whats wrong (because I don't...)
Thanks! :)
(PS: If you don't understand my english or if I have done something against the forum rules, please send me a PM, so it won't happen again.)
![Dev Dev](/uploads/1/2/6/1/126130250/392539005.jpg)
- 3 Contributors
- forum 3 Replies
- 9,312 Views
- 6 Hours Discussion Span
- commentLatest Postby Arne KristofferLatest Post
Nick Evan4,005
Very simple mistake:
if (str[x] 'a')
should be: if (str[x] 'a')
You are comparing each character from a string with another character, so you need single quotes instead of double
if I have done something against the forum rules
C++ String Compare Example
Actually, you are one of the few people who get the Code-tags right in their first post, so : bravo!
Dev C++ Download Windows 10
Edited by Nick Evan: n/a