#include <stdio.h>
// Retorna 0 se as strings sao iguais, -1 se forem diferentes
int CompareStrings(char* a, char* b);
int main()
{
// Ambas as strings tem quebras de linha
char firstTestA[] = "A casa era mais bonita \ndo que \nele imaginou\n";
char firstTestB[] = "A\n casa era mais bonita do que ele imaginou\n";
int comp = CompareStrings(firstTestA, firstTestB);
printf("Primeiro teste: %d\n", comp
);
// Uma das strings tem quebras de linha
char secondTestA[] = "A casa era mais bonita \ndo que \nele imaginou\n";
char secondTestB[] = "A casa era mais bonita do que ele imaginou";
comp = CompareStrings(secondTestA, secondTestB);
printf("Segundo teste: %d\n", comp
);
// Nenhuma das strings tem quebra de linha
char thirdTestA[] = "A casa era mais bonita do que ele imaginou";
char thirdTestB[] = "A casa era mais bonita do que ele imaginou";
comp = CompareStrings(thirdTestA, thirdTestB);
printf("Terceiro teste: %d\n", comp
);
return 0;
}
int CompareStrings(char* a, char* b)
{
int indexA, indexB;
for(indexA
= 0, indexB
= 0; indexA
< strlen(a
) || indexB
< strlen(b
); ++indexA
, ++indexB
) {
if(a[indexA] == '\n')
{
}
if(b[indexB] == '\n')
{
}
if((a[indexA] == '\0' || b[indexB] == '\0') || (a[indexA] != b[indexB]))
break;
}
// Se ambos terminaram, as strings contidas são iguais.
if( a[indexA] == '\0' && b[indexB] == '\0' )
return 0;
else
return -1;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgovLyBSZXRvcm5hIDAgc2UgYXMgc3RyaW5ncyBzYW8gaWd1YWlzLCAtMSBzZSBmb3JlbSBkaWZlcmVudGVzCmludCBDb21wYXJlU3RyaW5ncyhjaGFyKiBhLCBjaGFyKiBiKTsKCmludCBtYWluKCkKewogICAvLyBBbWJhcyBhcyBzdHJpbmdzIHRlbSBxdWVicmFzIGRlIGxpbmhhCiAgIGNoYXIgZmlyc3RUZXN0QVtdID0gIkEgY2FzYSBlcmEgbWFpcyBib25pdGEgXG5kbyBxdWUgXG5lbGUgaW1hZ2lub3VcbiI7CiAgIGNoYXIgZmlyc3RU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EsIGluZGV4QjsKICAgIGZvcihpbmRleEEgPSAwLCBpbmRleEIgPSAwOyBpbmRleEEgPCBzdHJsZW4oYSkgfHwgaW5kZXhCIDwgc3RybGVuKGIpOyArK2luZGV4QSwgKytpbmRleEIpCiAgICB7CiAgICAgICAgaWYoYVtpbmRleEFdID09ICdcbicpCiAgICAgICAgewogICAgICAgICAgICBpZigrK2luZGV4QSA+PSBzdHJsZW4oYSkpCiAgICAgICAgICAgICAgICBpbmRleEEgPSBzdHJsZW4oYSk7CiAgICAgICAgfQoKICAgICAgICBpZihiW2luZGV4Ql0gPT0gJ1xuJykKICAgICAgICB7CiAgICAgICAgICAgIGlmKCsraW5kZXhCID49IHN0cmxlbihiKSkKICAgICAgICAgICAgICAgIGluZGV4QiA9IHN0cmxlbihiKTsKICAgICAgICB9CgogICAgICAgIGlmKChhW2luZGV4QV0gPT0gJ1wwJyB8fCBiW2luZGV4Ql0gPT0gJ1wwJykgfHwgKGFbaW5kZXhBXSAhPSBiW2luZGV4Ql0pKQogICAgICAgICAgICBicmVhazsKICAgIH0KCiAgICAvLyBTZSBhbWJvcyB0ZXJtaW5hcmFtLCBhcyBzdHJpbmdzIGNvbnRpZGFzIHPDo28gaWd1YWlzLgogICAgaWYoIGFbaW5kZXhBXSA9PSAnXDAnICYmIGJbaW5kZXhCXSA9PSAnXDAnICkKICAgICAgICByZXR1cm4gMDsKICAgIGVsc2UKICAgICAgICByZXR1cm4gLTE7Cn0K