How to ignore case in a search

This is a discussion on How to ignore case in a search within the C Programming forums, part of the General Programming Boards category; You may have seen some other posts on the forum regarding a mileage chart. I am trying to do a ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    How to ignore case in a search

    You may have seen some other posts on the forum regarding a mileage chart.
    I am trying to do a search and want the search to ignore the input letter case.

    If I type in London, it finds London, however, if I type in london, it returns no result.

    The project was finished and handed in, but it just bothers me that I did not get this to work.

    Code:
    // Cproject.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include "string.h"
    
     
    
    //Function Prototypes
    
    int loc[10][10];
    
    void distances(int[10][10],char [10][11]);
    
    char towns[10][11];
    
    void townSearch(char[10][11]);
    
    void displayGrid();
    
    void cost(int [10][10],char [10][11]);
    
    int initialise(); 
    
    char menu();
    
    char option;
    
    int match;
    
    int arrayCount;
    
    char input,rtn;
    
    int arrayLocs[5];
    
    int distance;
    
    int totalCost;
    
    int longDist;
    
    /************************************************************************/
    
     
    
    int main ()
    
    {
    
      if (initialise()==0)
    
                      return 1;
    
                                    
    
      do
    
      {
    
                     system("cls");
    
                     fflush(stdin);
    
                     option = menu();
    
     
    
      switch(option)
    
     {
    
      case 'a':
    
                         displayGrid();
    
                                     system("pause");
    
             break;
    
      case 'b':
    
            distances(loc,towns);
    
                                    system("pause");
    
            break;
    
      case 'c':
    
            cost(loc,towns);
    
                                    system ("pause");
    
            break;
    
      case 'd':
    
            townSearch(towns);
    
                                    system ("pause");
    
            break;
    
      case 'e':
    
            return 1;
    
      default:
    
                      printf("Wrong input - Please Try Again\n\n");
    
                      system ("pause");
    
     }
    
    }while (option !='e');
    
     
    
    return 1;
    
     
    
    }
    
     
    
    /************************************************************************/
    
     
    
     
    
     
    
    int initialise()
    
    {
    
                    FILE *fp;                                               /*creates a file pointer named fp*/
    
                    int x,y, rowno=0;
    
     
    
                    if ((fp=fopen("C:\\Mileages.dat", "r")) == NULL)
    
                    {
    
                                    printf("\nFile could not be opened: Program terminating\n\n");
    
                                    system("pause");
    
                                    return 0;
    
                    }
    
                    else
    
                    {
    
                                    for (x = 0; x < 10; x++)
    
                                                    for (y = 0; y < 10; y++)
    
                                                                    fscanf(fp, "%d", &loc[x][y]);
    
                                    for (x = 0; x < 10; x++)
    
                                                    fscanf(fp, "%s", towns[x]);
    
                    }
    
                    fclose (fp);
    
                    return 1;
    
    }
    
     
    
    char menu()
    
    {
    
                    /* Menu Function */
    
                    char choice, rtn;
    
                    do
    
                    {
    
                       system("cls");
    
                       printf("What would you like to do ?\n\n");
    
                       printf(" a: Display mileage table\n\n");
    
                       printf(" b: Calculate distances between points\n\n");
    
                       printf(" c: Calculate cost of journey\n\n");
    
                       printf(" d: Search for a town in the list\n\n");
    
                       printf(" e: Exit\n\n");
    
                       scanf("%c%c", &choice, &rtn);
    
                       if (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'e')
    
                       {
    
                                       printf("\n\nWrong input, please try again\n\n");
    
                                       system("pause");
    
                       }
    
                    }while (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'e');
    
                    return choice;
    
    }
    
     
    
     
    
    void displayGrid()
    
    {
    
                    system("cls");
    
                    int x,y,rowno=0;
    
                    printf("   ");
    
                    for (x = 1; x < 11*10+3; x++)
    
                                    printf("_");
    
                    printf("\n  |          |");
    
                    for (x=0;x<10;x++)
    
                                    printf("%10s", towns[x]);
    
                    printf(" |\n ");
    
                    printf("  ");
    
                    for (x = 1; x < 11*10+3; x++)
    
                                    printf("_");
    
                    printf("\n");
    
                    for (x=0;x<10;x++)
    
                    {
    
                                    printf("  |%-10s|",towns[x]);
    
                                    for(y = 0;y < 10;y++)
    
                                    {
    
                                                    printf("%10d", loc[x][y]);
    
                                                    if(y!=0 && y % 9 == 0)
    
                                                    {
    
                                                                    printf(" |\n");
    
                                                    }
    
                                    }
    
                    }
    
                    // Base Line
    
                    printf("   ");
    
                    for (x = 1; x < 11*10+3; x++)
    
                                    printf("_");
    
                    printf("\n\n\n");                              
    
    }
    
     
    
    void distances(int loc[10][10],char towns[10][11])
    
    {
    
                    system("cls");
    
                    for (int x=0;x<10;x++)
    
                                    printf("%10s\n", towns[x]);
    
     
    
                                    match=0;
    
    do
    
    {
    
                    printf("Please enter the starting point for your journey from the above list\t");
    
                    scanf("%s%c", &input,&rtn);
    
                    for (int i = 0; i<10; i++)
    
                    {
    
                                                    if(strcmp(&input,towns[i]) ==0)
    
                                    {
    
                                                    arrayLocs[0] = i;                                                // Put start location as first element of array
    
                                                    match = 1;
    
                                    }
    
                    }
    
    }while(match != 1);
    
                    
    
                                    match=0;
    
    do
    
    {
    
                    printf("Please enter the destination point for your journey from the above list\t");
    
                    scanf("%s%c", &input,&rtn);
    
                    for (int i = 0; i<10; i++)
    
                    {
    
                                                    if(strcmp(&input,towns[i]) ==0)
    
                                    {
    
                                                    arrayLocs[4] = i;                                                                                // Put final destination as last element in array
    
                                                    match = 1;
    
                                    }
    
                    }
    
    }while(match != 1);
    
     
    
                                    
    
     
    
                                    printf("The direct distance between your chosen towns is  %d", loc[arrayLocs[0]][arrayLocs[4]]);
    
                                    printf("\tmiles\n\n");
    
     
    
                    printf("    Please enter any intermediate points along your journey - up to a maximum of three points\n");
    
                    printf("    Or type ""q"" to calculate the journey distance\n\n");
    
                    distance=0;
    
                    arrayCount=0;
    
                    int j;
    
                    j=0;
    
                    match=0;             
    
    while(j<3 && input !='q')                                                                                                                              // Loop three times or until q is entered
    
                    {              
    
            scanf("%s%c", &input,&rtn);                              
    
                                    for (int i = 0; i<10; i++)                                                                    //Iterate through list of towns
    
                                                    {
    
                                                                    if(strcmp(&input,towns[i]) ==0)
    
                                                                    {
    
                                                                                    arrayLocs[j+1] = i;                                                            // Start location is at arrayLocs[0], so next available space is arrayLocs[1]
    
                                                                                    distance=distance+loc[arrayLocs[j]][arrayLocs[j+1]];
    
                                                                                    arrayCount=j+1;                               
    
                                                                                    match=1;                                                             //Set flag to 1 to show the if statement has been entered
    
                                                                    }
    
                                                                                    
    
                                                    }              
    
                                                    if             (match==0 && input !='q')                           // If town entered is not in list, show error message
    
                                                                                    {
    
                                                                                                    printf("You may have miss-typed the location name. Please try again\n");
    
                                                                                                    printf("or type ""q"" to finish\n\n");
    
                                                                                                    j--;                                                                                                                          // Reset loop counter 
    
                                                                                    }              
    
                                                                                    else
    
                                                                                    {
    
                                                                                                    match=0;                                                             // Reset flag for next loop
    
                                                                                    }j++;                                                                                                      
    
                                    }
    
    distance=distance+loc[arrayLocs[arrayCount]][arrayLocs[4]];
    
    printf("The distance of your journey is now   %d",distance);
    
    printf("\tmiles\n\n");
    
     
    
    }
    
                    
    
    void townSearch(char towns[10][11])
    
    {
    
                    match=0;
    
                    // input of search for a place
    
                    printf("\nPlease enter a town to test if it is in the list\t ");
    
                    scanf("%s%c", &input,&rtn);
    
                    for (int i = 0; i<10; i++)
    
                    {
    
                                                    if(strcmp(&input,towns[i]) ==0)
    
                                                                    match = 1;
    
                    }
    
                                    if(match==1)
    
                                    {
    
                                                    printf("Your entry is in the list\n\n");
    
                                    }
    
                                    else
    
                                                    {
    
                                                    printf("Your selection is not in the list of towns\n\n");
    
                                                    }
    
                    }
    
     
    
     
    
    void cost(int loc[10][10],char towns[10][11])
    
    {
    
                            system("cls");
    
                for (int x=0;x<10;x++)
    
                            printf("%10s\n", towns[x]);
    
                            distance=0;
    
                            match=0;
    
                            double totalCost;
    
                            totalCost=0;
    
                            double longDist;
    
                            longDist=0;
    
    do
    
    {
    
                printf("Please enter the starting point for your journey from the above list\t");
    
                scanf("%s%c", &input,&rtn);
    
                for (int i = 0; i<10; i++)
    
                {
    
                                        if(strcmp(&input,towns[i]) ==0)
    
                            {
    
                                        arrayLocs[0] = i;                                   // Put start location as first element of array
    
                                        match = 1;
    
                            }
    
                }
    
    }while(match != 1);
    
                
    
                            match=0;
    
    do
    
    {
    
                printf("Please enter the destination point for your journey from the above list\t");
    
                scanf("%s%c", &input,&rtn);
    
                for (int i = 0; i<10; i++)
    
                {
    
                                        if(strcmp(&input,towns[i]) ==0)
    
                            {
    
                                        arrayLocs[4] = i;                                                           // Put final destination as last element in array
    
                                        match = 1;
    
                            }
    
                }
    
    }while(match != 1);
    
     
    
                            printf("The direct distance between your chosen towns is  %d", loc[arrayLocs[0]][arrayLocs[4]]);
    
                            printf("\tmiles\n\n");
    
          distance=loc[arrayLocs[0]][arrayLocs[4]];
    
                            //distance=50;
    
                 if(distance <= 100)      
    
                  {
    
                   totalCost = (distance*0.40);
    
                   printf("The cost is %.2f     ", totalCost);
    
                               printf(" pounds\n\n");
    
     
    
                  }
    
     
    
                   else
    
                  {
    
                   longDist = distance - 100;
    
                   longDist =(longDist * 0.30);
    
                   totalCost = (100 * 0.40) + longDist;
    
                  printf("The total cost of the journey is %.2f      ", totalCost);
    
                  printf(" pounds\n\n");
    
     
    
                  }
    
          
    
     }
    Anyone can show me would be much appreciated.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Without reading all your code (nor seeing a reason why you opted to post it) you can always use toupper() in a comparison, or even the stricmp() if you are using gcc. And windows has the CompareString() function. All of which can do case-insensitive comparisons.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know if this is what you're looking for, but what I do is simply turn all the locations into lowercase letters, regardless of how they're input.

    Then they're compared with cities that also have been changed to all lowercase.

    The user may see his input city as London, or Stoke on Trent, but the program just sees it as london or stoke on trent.

    So you don't ignore the case, you just manage it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21