C和C样式字符串中的Pascal样式字符串在一个函数中进行比较

我需要用C语言编写一个小函数,其中我有一个Pascal样式字符串和另一个字符串,C样式字符串。 在这个函数中,我应该比较这两个字符串,如果两个字符串相同,则函数应返回true,否则返回false。

int main() { char pascal_string // here I have a problem char c_string[] = "Home"; return 0; } 

好的,这样的?

 int compare(unsigned char *pascal, unsigned char *str, unsigned int strLength) { unsigned int length = pascal[0];// get length of pascal string if(length != strLength) return 0; // if strings have different length no need to compare for(int i = 0; i 

只需沿着每个arrays走下去。 无需预先计算C字符串的长度

 int same_pstring_cstring(const char *p, const char *c) { unsigned char plength = (unsigned char) *p++; // first `char` is the length while (plength > 0 && *c != '\0') { plength--; if (*p != *c) { return 0; } p++; c++; } // At this point, plength is 0 and/or *c is \0 // If both, then code reached the end of both strings return (plength == 0) && (*c == '\0'); } int main(void) { char pascal_string[1 + 4] = "\4" "Home"; char c_string[] = "Home"; char c_string2[] = "Home "; printf("%d\n", same_pstring_cstring(pascal_string, c_string)); printf("%d\n", same_pstring_cstring(pascal_string, c_string2)); return 0; }