0

I am trying to delete a node from the linked list. Node has a name that user enters. But I couldn't figure out how can I ignore the uppercase/lowercase in while loop. Here is my code.

void del(string e)
{
    temp=new node;
    temp=head;
    temp2=new node;
    temp2=temp->next;
    if(temp->info==e)
    {
        head=temp->next;
        delete temp;
    }
    else
    {
        while(temp2->info!=e)
        {
            temp=temp->next;
            temp2=temp2->next;
        }
        temp2=temp2->next;
        temp->next=temp2;
    }
}

And I get the string by this

cout<<"Enter the name to delete"<<endl;
ws(cin);
getline(cin,e);
del(e);

So is there any way to ignore uppercase/lowercase in while loop and if statement?

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
İrem Nur Arı
  • 71
  • 1
  • 1
  • 3

2 Answers2

0

The trick to non-cases sensitive comparison of two strings is to convert both of them either to lower or to upper case and then compare.

Unfortunately stl does not provide a very convenient way for the case conversions. So, here are some possibilities: https://stackoverflow.com/a/313990/1143850. just copying from there:

#include <algorithm>
#include <string> 

std::string data = "Abc"; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

so, in your case,

 string lowerE = std::transform(e.begin(), e.end(), e.begin(), ::tolower);    
 ...
 while(std::transform(temp2->info.begin(), temp2->info.end(), temp2->info.begin(), ::tolower) != lowerE) ...

Of course, you can create a function to simplfy it or use a different method of conversion there.

and another possibility definitely is to create you own comparison function and compare char by char, using the tolower or towlower function.

Serge
  • 11,616
  • 3
  • 18
  • 28
0

You don't need to manually convert the case of the strings being converted. If you're dealing with strings, use strcmp. For case insensitive checks, you can use _strcmpi.

E.g.

if(!strcmp(String1, String2)) { .... }

If strcmp returns 0 (FALSE) then there was a match, with case sensitivity applied.

For comparisons without case sensitivity, you use _strcmpi.

E.g.

#include <Windows.h>
#include <iostream>
using namespace std;

BOOL StringMatch(
    CONST CHAR *CmpString,
    CONST CHAR *CmpString2
)
{
    return (!_strcmpi(CmpString,
        CmpString2)) ? TRUE : FALSE;
}

int main()
{
    if (StringMatch("hello", "HELLO"))
    {
        cout << "Match without case sensitivity\n";
    }

    getchar();
    return 0;
}

Since you're using std::string, you can use the .c_str().

E.g.

string hellostring = "hello";

if (StringMatch(hellostring.c_str(), "HELLO"))
{
    cout << "Match without case sensitivity\n";
}

If you ever need to switch to Unicode encoding instead of Ascii, you have wcscmp and _wcs*/wcs*.