Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的Pascal样式字符串和一个函数中的C样式字符串比较_C_String - Fatal编程技术网

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

C中的Pascal样式字符串和一个函数中的C样式字符串比较,c,string,C,String,我需要用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, uns

我需要用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<length; i++)
        if(pascal[1+i]!=str[i])
            return 0;

    return 1;
}

int main(int argc, const char * argv[])
{
    unsigned char pascal[32] = {5, 'H', 'e', 'l', 'l', 'o' };
    unsigned char *ordinaryStr = "Hello";
    int t = compare(pascal, ordinaryStr, 5);// pass length of other string manually, e.g., 5
}
int比较(unsigned char*pascal,unsigned char*str,unsigned int strLength)
{
unsigned int length=pascal[0];//获取pascal字符串的长度
如果(长度!=strLength)
返回0;//如果字符串长度不同,则无需比较

对于(inti=0;i好的,像这样的东西

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<length; i++)
        if(pascal[1+i]!=str[i])
            return 0;

    return 1;
}

int main(int argc, const char * argv[])
{
    unsigned char pascal[32] = {5, 'H', 'e', 'l', 'l', 'o' };
    unsigned char *ordinaryStr = "Hello";
    int t = compare(pascal, ordinaryStr, 5);// pass length of other string manually, e.g., 5
}
int比较(unsigned char*pascal,unsigned char*str,unsigned int strLength)
{
unsigned int length=pascal[0];//获取pascal字符串的长度
如果(长度!=strLength)
返回0;//如果字符串长度不同,则无需比较

对于(inti=0;i只需遍历每个数组,无需预先计算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;
}

只需遍历每个数组,无需预先计算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;
}


嗨,giorgim,我需要一个没有库的函数。@dextrr所以…写一个。
strlen
是这个代码段中string.h中唯一的函数,它的实现很简单。是的,但不允许使用。谢谢giorgim。如果只有一个较长的Pascal样式字符串,并将此字符串拆分为两个字符串,您将如何编写函数该函数返回一个由一个字符串分割的两个字符串?@dextrr:首先创建字符串数组以保存这两个单独的字符串。开始迭代长Pascal字符串。您知道Pascal字符串中的第一个字符串有多长:检查第一个字节。现在复制尽可能多的字节(从长Pascal字符串复制)与第一个字节到第一个字符串数组一样。完成后,长pascal字符串中的下一个字节的长度将等于第二个字符串的长度,对本例执行与在firstHi giorgim中相同的操作,我需要一个没有库的函数。@dextrr so…编写一个。
strlen
是此代码段中string.h中唯一的函数,它的imple是的,但不允许使用。谢谢giorgim。如果您只有一个较长的Pascal样式字符串,并将此字符串拆分为两个字符串,并且该函数返回一个由这一字符串分割的两个字符串,您将如何编写函数?@dextrr:首先创建字符串数组以容纳这两个单独的字符串。开始迭代长Pascal字符串。您知道Pascal字符串中的第一个字符串有多长:检查第一个字节。现在复制尽可能多的字节(从长Pascal字符串)与第一个字节到第一个字符串数组一样。完成后,长pascal字符串中的下一个字节的长度将与第二个字符串的长度相同,在这种情况下执行与第一个相同的操作。如果只有一个较长的pascal样式字符串,则如何编写函数并将该字符串拆分为两个字符串,并且该函数返回两个字符串divi从这一个字符串中删除?chux这让我很困惑,为什么不只写char pascal_string[]=“\x4Home”?@dextrr如果pstring是4
字符,那么使用
“\x41234”
是一个问题,因为
“\x”
转义序列不在哪里停止。
“\x4”“1234”
OK,@chux,你将如何在一个数组中写入两个或多个Pascal字符串?你能解释这两行吗:while(plen&&c)return plen==*c;这对我来说非常有趣,如果你只有一个较长的Pascal样式字符串,并将此字符串拆分为两个字符串,而该函数返回一个从这一个字符串中分割出来的两个字符串,那么你将如何编写一个函数?chux这让我很困惑,为什么你不只编写char-Pascal\u字符串[]=“\x4Home”;?@dextrr如果pstring是4
char
“1234”
,那么使用
“\x41234”
是一个问题,因为
“\x”
转义序列不在哪里停止。
“\x4”“1234”
好的,@chux,您将如何在一个数组中写入两个或多个Pascal字符串?请解释这两行:while(plen&&c)return plen==*c;这很有趣,因为meComments不是用于扩展讨论的;此对话是。Comments不是用于扩展讨论的;此对话是。