Ring Documentation Walkthrough #8.2 – strcmp()

We can compare two strings using this function.

This is the example from the official documentation and the explanation by Eng. Mahmoud Fayed.

see strcmp ('hello', 'hello') + nl #output: 0, the two strings are typical.

see strcmp ('a', 'b') + nl #output: -1, the 'a' letter in ASCII is 97, 'b' in ASCII is 98, then 'a' < 'b' and the output is -1 

see strcmp ('b', 'a') + nl #output: 1, because 'b' > 'a' in ASCII.

But, in case you just want to see if the strings are the same or not we can do this:

give str

if strcmp (str, 'hello') = 0
   see `The string you entered does match.`
else
   see `The string you entered doesn't match.`
ok

NOTE: In the if statement here, we use only one = not == like in the other languages.

Then test it:

input: hello
output: The string you entered does match.
input: Hello
output: The string you entered doesn't match.

This is basically how it is done.

See you in the next post.

amrESSAM.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s