Thread: comparing strings: case INsensitive

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    comparing strings: case INsensitive

    I'm trying to write a program that takes in a list of names and ages, then searches for a certain first name and last name. However, in the problem prompt, it requires that the comparison between the name and search value to be case insensitive. I've tried stricmp() but I don't think the compiler I'm using uses it (GCC). Here's my code for my search() function so far.

    Code:
    int search( char names[20][20], int ages[20], char value[20], int *totAges )
    {
      char tempArr[20];
      int rCtr = 0;
      int cCtr = 0;
      int ctr = 0;
      int ageCtr = 0;
    
      for( rCtr = 0; rCtr < 20; rCtr++ ) {
        strcpy( tempArr, names[rCtr] );
        if( !strcmp( tempArr, value ) ) {
          ctr++;
          printf( "%d\n", ages[rCtr] );
          ageCtr += ages[rCtr];
        }
      }
      *totAges = ageCtr;
    
      return ctr;
    }
    Any help would be greatly appreciated. Thanks!

  2. #2
    Banned
    Join Date
    Dec 2008
    Posts
    49
    strcasecmp()

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    8
    Thanks! Works perfectly now.

  4. #4
    Banned
    Join Date
    Dec 2008
    Posts
    49
    No problem. Just be aware that strcasecmp() is not portable. So outside of GCC your code may not even work. But its all good, you didn't seem like that was an issue.

  5. #5

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And many other compilers have "stricmp" that does the same thing.

    And it's not terribly hard to do the same thing if you need to.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    How comes there is stricmp() in MinGW?
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    MinGW uses the Microsoft CRT (that comes with VC 6.0, msvcrt.dll), and exposes all of its functionality.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  2. Strings and Switch Statement
    By Ajsan in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2004, 01:16 PM
  3. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM