Write a function to compare two C-strings for equality. The function should return true if the strings are equal and false if they are not. Your function should ignore case, punctuation, and whitespace characters. Test your function with a variety of input strings.
[Note: Do not use the standard library function strcmp(). ]
I dont know How to compare two strings.
Please help me.
English isnt my mother language , so there is some grammer mistakes,maybe.
Buddy, i can tell u the logic,just read two strings character by character, store these characters in say tempch1,tempch2 of character type. compare them using == operator, if they are equal proceed with further comparison else report that they arent equal.(also add checks for conditions u mentioned.)
If two strings are exactly equal, a smart compiler will not duplicate the string if it's exactly the same as another string. Therefore, this code could work, depending on the intelligence of your compiler:
The condition compares the addresses of the first character of the string. If the compiler did in fact use the same string, the addresses would be same. Thus, the strings are equal. However, not all compilers are clever, so it's necessary to compare each character 1 by 1 as coder777 said.
You can imagine how other punctuation might also be ignored. It should be noted that this function destroys the original strings (by converting all upper case letters to lower case). This could be solved by copying the strings a and b to new locations in memory and freeing them at the end of the function, but I'll leave that as an exercise for the reader.
Implementing this in an actual program would be as simple as this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <cstdio>
#include <cstdlib>
bool compare(char *a, char *b); //Replace with the function from the snippet above.
int main(){
char *a, *b;
a=(char*)calloc(256,sizeof(char));
b=(char*)calloc(256,sizeof(char));
printf("Enter two lines of text.\n");
fgets(a,256,stdin);
fgets(b,256,stdin);
if(compare(a,b))printf("The strings match, ignoring case, space, and periods.\n");
else printf("The strings do not match.\n");
return 0;
}