You can use this code for to compare strings without case sensitive and not using strcmpi(). I created a “convert” method for convert chars to upper case.
The toupper()
function converts ch to its uppercase version if it exists. If the uppercase version of a character does not exist, it remains unmodified. The lowercase letters from a to z is converted to uppercase letters from A to Z respectively.
The behaviour of toupper()
is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include<stdlib.h> #include <stdio.h #include <ctype.h> using namespace std; string convert(string s) { for(int i=0;i<s.length();i++) { s[i]=toupper(s[i]); } return s; } int main() { string s1,s2; cout<<"String 1 : "; cin>>s1; cout<<"String 2 : "; cin>>s2; if(convert(s1)==convert(s2)) { cout<<"Two strings is equal"; } else { cout<<"Two strings is not equal"; } } |
When you run the program, the output will be: