Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Comparing Characters in Strings

c string

  • Please log in to reply
4 replies to this topic

#1 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 03 November 2012 - 11:22 AM

How can I compare two characters to see if they are the same letter (regardless of caps)



thus the following will not work

if(string[f] == string[b]){

}


since string[f] could be 'A' and string[b] could be 'a'

and I need that to return in true



thank you!
  • 0
Your Friendly Neighborhood Pally

#2 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 03 November 2012 - 11:30 AM

You can lower case them all then compare them.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#3 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 03 November 2012 - 11:52 AM

Well, standard string library isn't as good as String library in Qt (QString) or C#, or java strings. There is no ( that I know ) method in standard string library to lower or upper case characters. Howewer you might wan't to write your own, since it's not so hard.
All you will need is ASCII table like one here :
http://www.asciitable.com/

function to lower would look like :
string toLower(string str) {
    for (int i=0;i<str.length();i++)
	    if ( (str[i] >= 65) && (str[i] <= 90) )
		    str[i] = str[i] + 32;
    return str;
}
As you see I am comparing ASCII values(decimal ones) of each character in passed string.
then if value is out of lower's character range I'm incrementing it's ASCII value.
Go on check it out and try to make own toUpper function.
  • 0
Remebre about KISS & DRY

#4 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 03 November 2012 - 11:58 AM

alright I just did the conversion via ascii


thank you


solved
  • 0
Your Friendly Neighborhood Pally

#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 03 November 2012 - 05:46 PM

Well, standard string library isn't as good as String library in Qt (QString) or C#, or java strings. There is no ( that I know ) method in standard string library to lower or upper case characters.As you see I am comparing ASCII values(decimal ones) of each character in passed string.
then if value is out of lower's character range I'm incrementing it's ASCII value.
Go on check it out and try to make own toUpper function.


Um...
#include <ctype.h> // If you're using C
#include <cctype> // If you're using C++

void strlower(char *s)
{
  for( ; *s != '\0'; ++s )
    *s = tolower(*s);
}

The great thing is, if *s isn't a letter or other character that doesn't have a lowercase version, nothing will be changed, so you don't have to do an if statement.

As for your original question, you can use stricmp in the standard C library. Include string.h.

It's a good idea to do as little modification of the inputs as possible; in fact, you shouldn't unless it's absolutely necessary. What I recommend is this:

bool compare_nocase(const char *a, const char *<img src='http://img.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />
{
  while( (*a != '\0') && (*b != '\0') )
  {
    if( tolower(*a) != tolower(*<img src='http://img.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> )
      return false;

    ++a;
    ++b;
  }

  return *a == *b;
}

  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: c, string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download