How to implement case insensitive fncs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tienweiwei
    New Member
    • Mar 2010
    • 1

    How to implement case insensitive fncs?

    Implement AccessIgnoreCas e() to count the number of stations from the head to a destination using linked list.
    for e.g.
    PoLam->HangHau->TseungKwanO->TiuKengLeng->YauTong->QuarryBay->NorthPoint->NIL

    Implement Access() which is case sensitive. However, it crashes when input does not match the string. How can I resolve this and how can I write a AccessIgnoreCas e() which is case insensitive?


    int List::Access(st ring str)
    {
    if (m_Head == NULL)
    return -1;
    else
    {
    if (m_Head->str == str)
    return 0;
    else
    {
    int counter = 0;
    Node* current;
    current = m_Head;

    while (current!= NULL)
    {
    counter++;

    if (current->ptrNext->str == str)
    return counter;
    else
    current = current->ptrNext;
    }
    if (current == NULL)
    return counter;
    }
    return -1;
    }
    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Here's your problem:
    if (current->ptrNext->str == str)
    All you know is current != null.
    You don't know if current->ptrNext ?= null, which it does when the program crashes.
    Change it to
    if (current->str == str)
    and adjust your counter accordingly, eg maybe put the increment at the end of the loop.

    Use a string function to convert both strings to lower case. eg:

    Comment

    Working...