Jump to content
Science Forums

Recommended Posts

Posted

I saw somwhere in example on how to handle strings that for Alpha and Omega being strings:

Alpha > Omega = true

 

Why is this? They have the same number of letters... I also tried to write the following program:

#include <iostream>
#include <string>
using namespace std;
int main()
{
 cout<<"hello world";
 string str1 ("Alpha");
 string str3 ("Omega");

 if(str3>str1) cout << "str1 > str3 n";
 else cout << "ho ragione io";
} 

 

And the output was indeed str1 >str3

Posted

I don’t have a C compiler handy at the moment, and haven’t touched c code in over a year, but I believe your problem is that you’re using the “>” operator to compare 2 type “string” variables. String variables is plain c are just pointers (typically 32 bit unsigned integers) to an array of character (8 bit) values. So,

if(str3>str1) ;

is actually saying “if the address of the string pointed to by str3 is greater than the address of the string pointed to by str1”.

 

You should use the strcmp() function, which is conventionally in string.h, eg:

if (strcmp(str3,str1) != 0) ;

 

There’s an old, multi-language code joke that involves comparing the strings “Alpha” and “0mega” (zero in place of O), but this doesn’t seem to be an instance of that.

Posted

I think you're right, CraigD. In fact, it was stuff like this that got me so annoyed at the standard string variable that I wrote my own which, while it doesn't work perfectly with everything yet, is far more intuitive (at least for me) i.e. concatenation is done using the '+' operator, strings can be compared using the logical operators, and things of that nature.

Posted
In fact, it was stuff like this that got me so annoyed at the standard string variable that I wrote my own which, while it doesn't work perfectly with everything yet, is far more intuitive (at least for me) …
There are a whole slew of satirical programming folk aphorism along the lines of:
  • “C combines the flexibility of assembly language with the power of assembly language”;
  • “C offers the power of assembly language with the programming ease of assembly language”

Personally, I prefer to program in either assembly language (expanding macros), or in a high-level interpreted language. “In between” language like C and C++, with weak and strong object orientation and other forms of “data and code hiding”, fill me with unease and trepidation.

 

My hope for the future of computing is a resurgence of pure-interpreter computing platforms similar to an unsophisticated view of pre-PC computers like the Apple 2 and the TRS 80, with all the language primitives implemented in hardware. With processing power abundant, I think this is now a practical, achievable vision, offering profound benefits. I’d best stop now, lest I begin preaching my “gospel of code interpreters” :lol:

Posted

No actually the aim of the string class included in the start of the program was to be able to use such operators as ">,<,=,+,+=,...". And my program is the exact copy of one on a book where they explain the string class. They just say that such strings can be compared but not what is the criteria.

For the "+" operater is quite easy str1 + str3 gives as output AphaOmega...

Isn't it maybe besed on the value of the letter composing the string?

Posted
No actually the aim of the string class included in the start of the program was to be able to use such operators as ">,<,=,+,+=,...". And my program is the exact copy of one on a book where they explain the string class. They just say that such strings can be compared but not what is the criteria.
I’ve used C++ only working through textbook exercises and reading documentation, but for any sort of standard class, like string, to be useful, I agree, it should compare strings in the ordinary way. Still, the behavior you describe sounds as if it’s comparing the variables as integer pointer values, as Dave and I suspect.

 

Try this experiment: switching the order in which and values to which you instantiate str1 and str3. If the order changes the program outputs, but the values don’t, I suspect the “>” operator is comparing their addresses, not the strings they reference.

 

To be sure, you’d have to look at how the class defines (“overloads”) the “>” operators. I’m not sure if you can do that by examining the included files (I don’t have any such files handy), but that’s worth a try. Is there any possibility that string.cls is not the usual unusual version?

Posted
I saw somwhere in example on how to handle strings that for Alpha and Omega being strings:

Alpha > Omega = true

I had taken this for granted, but :lol:

 

 string str1 ("Alpha");
 string str3 ("Omega");

 if(str3>str1) cout << "str1 > str3 n";

 

And the output was indeed str1 >str3

It's perfectly as I would expect for a reasonable overload of > for a string class, it seems to be alphabetical order!

 

"A" < "O"

 

"O" > "A"

Posted
 if(str3>str1) cout << "str1 > str3 n";

… And the output was indeed str1 >str3

:lol: What is this crass trickery?!

 

Of course this code will output “str1 > str3” – the outputted string literal has swapped the order of the variable actually being compared!

 

I’m curious, sanctus – was this a typographical error on you part, or an experiment to determine how long it would take your fellow hypographers to find the obvious?:stupid:

Posted
an experiment to determine how long it would take your fellow hypographers to find the obvious?:)
And with a pretty narrow [math]\norm\sigma[/math], less than a quarter hour! :D
Posted

No it was a typo, it should be:

if (str3>str1) cout<<"str3<str1";

But I still do not know why Omega is bigger than Alpha, unless it is alphabetical order.

Posted
But is it the sum or just the first letter?
Every ordinary fixed character length string ordering algorithm I know is something like this (assumes NUL delimited strings):
strcmp(a,B) // returns 1 if a>b, -1 if a<b, 0 if a=b
char a[],b[]
{ int i;
 for (i=0;; ++i) {
   if (a[i] > b[i]) return (1);
   if (a[i] < b[i]) return (-1);
   if (a[i] == '0') return (0);
}}

Pretty much the way a human being alphabetizes printed text.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...