A homework I cant complete

I am a beginner in C++.

There was a homework that makes a big trouble.

Please help me.

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.

Last edited on
You need a function where you pass 2 c-strings, e.g.: bool CompareStrings(const char *s1, const char *s2)

Then you need a loop where you compare the chars:
1
2
3
4
while(...)
{
  if(s1[i] != s2[i]) ...
}


if an inequality is found set the result to false and break the loop. And Check the 0
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.)
closed account (zb0S216C)
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:

1
2
3
4
5
6
7
const char *String1("A string");
const char *String2("A string");

if(String1 == String2)
    std::cout << "Get in there, my son!\n";

else std::cout << "Oh well\n";

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.

Wazzak
Last edited on
If the OP wants to compare C-strings, it would seem like using pointers is the ideal solution. I would approach the problem like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Compare two strings. Return true if they match, else false.
//Ignore spaces, periods, and case.
bool compare(char *a, char *b){
	char *sa, *sb;
	for(sa=a,sb=b; *sa && *sb; sa++,sb++){
		while(*sa){
			if(*sa=='.' || *sa==' ')sa++;
			else {
				if(*sa>=65 && *sa<=90)*sa+=32;
				break;
			}
		}
		while(*sb){
			if(*sb=='.' || *sb==' ')sb++;
			else {
				if(*sb>=65 && *sb<=90)*sb+=32;
				break;
			}
		}
		if(*sa != *sb)return false;
	}

	return true;
}


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;
}
Last edited on
Topic archived. No new replies allowed.