Hi all! I am making a program to input a paragraph from the user and then finds/swap/delete/insert a word in that paragraph. I am stuck on the first stage. I have made a program that just returns the starting index of the searched word. But its case sensitive and i want to search case insensitive word. Can someone guide me plz?

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char str[500],find[500],option;
cout<<"Enter a text paragraph: ";
cin.getline(str,500,'$');
cout<<"\n\t\t String Operations "<<endl;
cout<<"\t Find\t\t:\t\t[F]"<<endl;
cout<<"\t Replace\t:\t\t[R]"<<endl;
cout<<"\t Delete\t\t:\t\t[D]"<<endl;
cout<<"\t Insert\t\t:\t\t[I]"<<endl;
cout<<"\t DeleteSpecial\t:\t\t[S]"<<endl;
cout<<"\t Print\t\t:\t\t[P]"<<endl;
cout<<"\t Quite\t\t:\t\t[Q]"<<endl;
cout<<"\t Enter Option\t:\t";
cin>>option;
cin.ignore(100,'\n');
while (!(option=='F' || option=='f' || option=='R' || option=='r' || option=='D' || option=='d' || option=='I' || option=='i' || option=='S' || option=='s' || option=='P'|| option=='p'|| option=='Q'|| option=='q'))
{
cout<<"\nInvalid option!"<<endl<<endl;
cout<<" Enter One of these Options only"<<endl<<endl;
cout<<"\t Find\t\t:\t\t[F]"<<endl;
cout<<"\t Replace\t:\t\t[R]"<<endl;
cout<<"\t Delete\t\t:\t\t[D]"<<endl;
cout<<"\t Insert\t\t:\t\t[I]"<<endl;
cout<<"\t DeleteSpecial\t:\t\t[S]"<<endl;
cout<<"\t Print\t\t:\t\t[P]"<<endl;
cout<<"\t Quite\t\t:\t\t[Q]"<<endl;
cout<<"\t Enter Option\t:\t";
cin>>option;
cin.ignore(100,'\n');
}
switch(option)
{
case 'F': case 'f':
{
cout<<"Enter the word to find: ";
cin.get(find,100);
cin.ignore(100,'\n');
int wordlen=strlen(find);
for (int i=0;str[i];i++)
{
if (str[i]==find[0])
{
for (int j=0;find[j];j++)
{
if (str[i]==find[j])
{
if ((j+1)==wordlen)
{
cout<<find<<" is found at index "<<(i+1-wordlen)<<endl;
}
i++;
}
}
}
}
}
}
return 0;
}

But its case sensitive and i want to search case insensitive word. Can someone guide me plz?

When you want a character comparison that's case insensitive, you can convert both characters to either upper or lower case. For example:

#include <cctype> // For toupper/tolower

...

if (tolower(str[i]) == tolower(find[0]))

The reason this works is tolower() will simply return the argument if there's no lower case conversion, so you'll either get the lower case variant of the character if one exists, or the character itself if no such conversion exists.

If you end up using a library to do the comparison then it gets a bit trickier because some libraries (such as strcmp()) don't support any way of enabling a case insensitive comparison. But that's not currently relevant because you're doing the comparison manually. :)

This article has been dead for over six months. Start a new discussion instead.