<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Case Insensitive compare</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/22651/case-insensitive-compare</link><description> 
hi 
how can you do a case insensitive string compare ? 
i&amp;#39;m using uvision3 , c51 version 8. 
the string.h don&amp;#39;t know stricmp or strlwr. 
can i compile my own function ? 

 
thanks 
gamba 
 </description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/138575?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 15:53:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0c9d9333-9e0f-42f5-a100-fd7a127067bb</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
You are overcomplicating things.&lt;/p&gt;

&lt;p&gt;
I think you should consider looking for a good book on programming
in C - it seems like you are not familiar with C or C++.&lt;/p&gt;

&lt;p&gt;
By the way - is your posted source code a verbatim copy, or have
you typed in the code again? The warning is about stricmp, I can&amp;#39;t
see any calls to stricmp - just a call to lwrbuffer() and strcmp().
My guess is that you hand-typed, and that the strcmp() should be
stricmp(). Never type in the code when posting. Always select all
code and then copy/paste. I repeat: Always copy/paste!&lt;/p&gt;

&lt;p&gt;
Anyway, this is a very incomplete list with problems in your
program:&lt;/p&gt;

&lt;p&gt;
In main(), you are calling lwrbuffer() without having declared the
prototype. Any functions that you create must either have their
function body above the function that calls it, our you must
explicitly add a function prototype - either at top of the source
file or in one of your include files.&lt;/p&gt;

&lt;p&gt;
lwrbuffer() has return type void, which doesn&amp;#39;t mean &amp;quot;returns any
type&amp;quot; but means &amp;quot;does not return anything&amp;quot;. So, you can&amp;#39;t in main()
just typecast the return from lwrbuffer() to (char*). There is
nothing returned!&lt;/p&gt;

&lt;p&gt;
lwrbuffer() has return type void, but still contains code of the
type return &amp;lt;value&amp;gt;. A function with return type void must
either be without any return statement, or may only contain empty
return statements, i.e.&lt;/p&gt;

&lt;pre&gt;
void fnk(void) {
    return;
}
&lt;/pre&gt;

&lt;p&gt;
lwrbuffer() is using old K&amp;amp;R-style parameter declaration, i.e.
only the parameter name is specified. An you are following that up by
the parameter being implied int. Your code should be:&lt;/p&gt;

&lt;pre&gt;
char* lwrbuffer(const char* checkbody) {
    ...
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
By the way - why calling the parameter checkbody? A function called
lwrbuffer by definition should not need to know WHY you convert a
string to lowercase - so why should the parameter imply that it is
for some form of check?&lt;/p&gt;

&lt;p&gt;
main() constains the following code:&lt;/p&gt;

&lt;pre&gt;
char *path= &amp;quot;hello&amp;quot;;
textBody = (char*)strchr(path,&amp;#39;\0&amp;#39;);
textBody++;
strcpy(textbody2,textBody);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
You first locate the terminating zero of the &amp;quot;hello&amp;quot; string, and then
steps one step past. What do you expect to find there? That is an
undefined memory location, so you can/must not try to perform that
strcpy() operation!&lt;/p&gt;

&lt;p&gt;
You have the following line:&lt;/p&gt;

&lt;pre&gt;
scanf(&amp;quot;%d&amp;quot;, &amp;amp;n);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
It is expected to get characters from stdin (which may be an
interesting concept for an embedded system) and scan for an integer.
Shouldn&amp;#39;t you check the return value of scanf() to see if an integer
was received?&lt;/p&gt;

&lt;p&gt;
You are making quite a lot of copying - why? You can compare two
strings on-the-fly without any copying. Just use two character
pointers - one for each string. Convert the first character from each
pointer to upper (or lower) case and compare. If different, you have
an answer. If not different but any of the pointers point to a &amp;#39;\0&amp;#39;
then you have reached the end of both strings, and they where equal.
If not different and neither pointer pointed to &amp;#39;\0&amp;#39;, then step both
pointers forward one step and repeat. Quick and easy with just a
couple of code lines and no extra buffer space needed. Also, the
comparison code will terminate as soon as possible in case the
strings where not identical - why convert the full string to
lowercase if it was enough to look at the first character?&lt;/p&gt;

&lt;p&gt;
your code:&lt;/p&gt;

&lt;pre&gt;
for (;i &amp;lt;= strlen(checkbody);i++) {}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
will call strlen() once for every character in checkbody(). And
strlen() will then have to step through every character in
checkbody... For a 100-character string, the inner loop in strlen()
will have to step 100*100 = 10000 times! No need for an O(n^2)
solution to an O(n) problem. No need for a separate loop variable or
knowing the length of the string. Since you are scanning through the
string one character at the time anyway - just let the loop end when
the current character is &amp;#39;\0&amp;#39;.&lt;/p&gt;

&lt;p&gt;
The lwrbuffer() function is converting the input string to
lowercase in-place. So why are you at the same time assigning the
lower-case conversion into the lowertextBody variable? Also note that
the lowertextBody pointer has never been assigned an initial value,
so it doesn&amp;#39;t point to a buffer that can store any values.&lt;/p&gt;

&lt;p&gt;
You continue with even one more &amp;quot;extra&amp;quot; string copy,
doing:&lt;/p&gt;

&lt;pre&gt;
strcpy (lwrtext,lowertextBody);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
But this time too, the lwrtext pointer hasn&amp;#39;t been initialized, so
there are no buffer to store the string.&lt;/p&gt;

&lt;p&gt;
I really, really recommend that you pick up a good book on C - you
can&amp;#39;t do embedded programming if you are not comfortable writing code
on a PC.&lt;/p&gt;

&lt;p&gt;
Drop your over-complicated code and sit down a bit and think about
implementing a stricmp() yourself. I have already mentioned the
required steps in the algorithm. They are few. They are simple. So
the stricmp() function must by deduction also be simple.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/149288?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 15:44:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8d7839b6-8410-41c0-8ae9-c7ae64861b13</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Well done for using the correct tags to post your source code, but
is that really how you lay it out?&lt;/p&gt;

&lt;p&gt;
Your indenting is quite bizarre!&lt;br /&gt;
Have you used TABs?&lt;br /&gt;
Don&amp;#39;t use TABs, use spaces - TABs are entirely unreliable!&lt;/p&gt;

&lt;pre&gt;
void lwrbuffer(checkbody)
{
  int   i;
  char *lwrtext;
  char *lowertextBody;

  i=0;

  for (;i &amp;lt;= strlen(checkbody);i++)
  {
    checkbody[i] = (checkbody[i]&amp;gt;=&amp;#39;a&amp;#39; &amp;amp;&amp;amp; checkbody[i]&amp;lt;=&amp;#39;z&amp;#39;) ?
                   checkbody[i]-(&amp;#39;a&amp;#39;-&amp;#39;A&amp;#39;) : checkbody[i];

    lowertextBody[i] = checkbody[i];
  }
  strcpy (lwrtext,lowertextBody);
  return (lwrtext) ;
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
You haven&amp;#39;t defined the type of the parameter to lwrbuffer - &amp;#39;C&amp;#39; will
make an assumption, but it isn&amp;#39;t what you want;&lt;/p&gt;

&lt;p&gt;
Why do you initialise i to zero in a separate statement outside
the for loop?&lt;/p&gt;

&lt;p&gt;
You are converting each element of the checkbody array in-place -
there is no need for all the messing about with copying to
lowertextBody and lwrtext;&lt;/p&gt;

&lt;p&gt;
You are treating lowertextBody and lwrtext as if they were arrays
- but they are just pointers;&lt;/p&gt;

&lt;p&gt;
You are returning a value in a void function.&lt;/p&gt;

&lt;p&gt;
Note that this is all standard &amp;#39;C&amp;#39; - nothing specific to Keil nor
the 8051 here!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/130210?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 15:10:33 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:45bbd9be-b3ff-4002-925a-9a015b54a052</guid><dc:creator>gamba lo</dc:creator><description>&lt;p&gt;&lt;p&gt;
i understand that i am supposed to add the stricmp() to the c51
program somehow but i failed.&lt;/p&gt;

&lt;p&gt;
file.C(101): error C267: &amp;#39;stricmp&amp;#39;: requires ANSI-style
prototype&lt;/p&gt;

&lt;p&gt;
file.C(101): warning C206: &amp;#39;stricmp&amp;#39;: missing
function-prototype&lt;/p&gt;

&lt;p&gt;
this is all i get.&lt;/p&gt;

&lt;p&gt;
i used my own function to receive input and transform it to
lower-case but when i call the function i get the same errors as
above.&lt;/p&gt;

&lt;p&gt;
if i change the function to void it runs, but if i deliver a
variable to the function i get the error above.&lt;/p&gt;

&lt;p&gt;
the code is&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;REG52.H&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;ctype.h&amp;gt;

void main (void)
{
        char *textBody;
        char *textbody2;
        char *password;
        char *lwrtext2;
        char *checkbody;
        char *path= &amp;quot;hello&amp;quot;;
        textBody = (char*)strchr(path,&amp;#39;\0&amp;#39;);
        textBody++;

        strcpy(textbody2,textBody);
        lwrtext2 = (char *)lwrbuffer(textbody2);
                        if (strcmp(lwrtext2, &amp;quot;password&amp;quot; )==0)
                        printf(&amp;quot;password OK!&amp;quot;);
        scanf(&amp;quot;%d&amp;quot;, &amp;amp;n);
        }

void lwrbuffer(checkbody)
  {
  int i;
  char *lwrtext;
  char *lowertextBody;
  i=0;
  for (;i &amp;lt;= strlen(checkbody);i++)
  {
checkbody[i] = (checkbody[i] &amp;gt;= &amp;#39;a&amp;#39; &amp;amp;&amp;amp; checkbody[i] &amp;lt;= &amp;#39;z&amp;#39;) ? checkbody[i] - (&amp;#39;a&amp;#39;-&amp;#39;A&amp;#39;) : checkbody[i];
        lowertextBody[i] = checkbody[i];
        }
        strcpy (lwrtext,lowertextBody);
        return (lwrtext) ;
        }
&lt;/pre&gt;

&lt;p&gt;
Any thoughts ?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/124148?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 09:51:34 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:125288c3-a17a-4a71-8a78-1b5bc3d1f3b0</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;it seems that the C51 is not supporting any function of lower
or upper case convert.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
stricmp() and its relatives are not standard C library functions.
If I recall correctly, they&amp;#39;re defined by one of the POSIX
standards.&lt;/p&gt;

&lt;p&gt;
FreeBSD source for their C library can be found here.&lt;br /&gt;
&lt;a href="http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/"&gt;www.freebsd.org/.../&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/113282?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 01:32:50 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e52cf5ec-a34d-4562-bc10-fd5d9b03d0c3</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;pre&gt;
c = (c &amp;gt;= &amp;#39;a&amp;#39; &amp;amp;&amp;amp; c &amp;lt;= &amp;#39;z&amp;#39;) ? c - (&amp;#39;a&amp;#39;-&amp;#39;A&amp;#39;) : c;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Yes, that&amp;#39;s a neat way to &lt;i&gt;convert&lt;/i&gt; from lower- to upper-case;
it is left as an exercise for the student to apply a similar
expression to make a case-insensitive &lt;i&gt;compare&lt;/i&gt;...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/99768?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 01:18:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0b952300-926c-4b48-9ece-4e3f4a43d00a</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
How about just:&lt;/p&gt;

&lt;pre&gt;
c = (c &amp;gt;= &amp;#39;a&amp;#39; &amp;amp;&amp;amp; c &amp;lt;= &amp;#39;z&amp;#39;) ? c - (&amp;#39;a&amp;#39;-&amp;#39;A&amp;#39;) : c;
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/99762?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 01:16:15 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0f0f0bc3-61e7-4a98-bd17-f398eea47478</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;it seems that the C51 is not supporting any function of lower
or upper case convert.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Assumed that you are using the ASCII code, then converting between
upper and lower case is a simple matter of adding or subtracting an
offset from the character.&lt;/p&gt;

&lt;p&gt;
For example:&lt;/p&gt;

&lt;p&gt;
&amp;#39;A&amp;#39; = 0x41&lt;br /&gt;
&amp;#39;a&amp;#39; = 0x61&lt;br /&gt;
&amp;#39;a&amp;#39; - &amp;#39;A&amp;#39; = 0x20&lt;/p&gt;

&lt;p&gt;
So in order to convert an upper case letter to lower case, just
add 0x20. To convert lower to upper case, just subtract 0x20. Of
course, you need to make sure that the character in question is
actually a letter and not some other character, or you&amp;#39;ll convert
your string to garbage.&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://en.wikipedia.org/wiki/Ascii"&gt;en.wikipedia.org/.../Ascii&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/75591?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 01:00:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:dd574df5-49b7-4f00-a9e6-b91c947ec264</guid><dc:creator>gamba lo</dc:creator><description>&lt;p&gt;&lt;p&gt;
hi&lt;br /&gt;
it seems that the C51 is not supporting any function of lower or
upper case convert.&lt;br /&gt;
please advice.&lt;/p&gt;

&lt;p&gt;
thanks everyone!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/51203?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 00:26:43 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2eaff90d-74b8-4624-985d-ffc809b99bd4</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;how can you do a case insensitive string compare ?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
A simple solution is to convert all letters in the strings to one
case (upper or lower), and then do a regular string compare.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/99761?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 00:24:10 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d1159da3-d83f-4f0c-845d-7b68128d095c</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Just note that open-source isn&amp;#39;t the same as free source. If you
are working with a commercial project - be careful with GPL-licensed
code, since it taints your whole application with the GPL license. It
is only the special case of linking with a LGPL:ed library that the
tainting doesn&amp;#39;t occur.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/88591?ContentTypeID=1</link><pubDate>Fri, 12 Oct 2007 00:17:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a3b8537c-67dd-4bf1-9cdb-fa2826d83eca</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
There are plenty of open-source compilers - you could take a look
at their implementations...&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://codesearch.google.com"&gt;http://codesearch.google.com&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Case Insensitive compare</title><link>https://community.arm.com/thread/51199?ContentTypeID=1</link><pubDate>Thu, 11 Oct 2007 23:26:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:be06f046-3244-4b62-a681-1717096a87cb</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;can i compile my own function ?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Only you can answer that question ;)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>