C 在数组中添加和搜索解析数据

发布于 2024-11-14 02:22:48 字数 3169 浏览 4 评论 0原文

我正在尝试将字符串解析为较小的字符串,提取一些值,然后我想检查这些值中的任何一个是否是重复的...

这是我蹩脚的代码:)

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="INVITE sip:[email protected] SIP/2.0\nCall-ID: [email protected] To: <sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;";
  char * tch;
  char * saved;  
char * array[50];
int count = 0;        
  tch = strtok (str,"<:;>");
    while (tch != NULL)
  { 
    int savenext = 0;              
    if (!strcmp(tch, "sip"))   
    {                             
      savenext = 1;                
    }                               
    printf ("%s\n",tch);
    tch = strtok (NULL, "<:;>");
    if (savenext == 1)             
    {                              
      saved = tch;                  
    }                              

if ( count == 0 ) {
    array[count] = saved;  
    count ++;  
    }
    if ( count > 0 ) {
        int i = 0;
        while (i < count ) {
            if (array[count] == saved ) {
                printf("FOUND!");
                }
                i++;}
                }


            }
 }

我要做的是检查是否有相同的用户名在字符串中找到了两次,但我缺乏指针经验,阻止了我这样做。我不明白为什么这些值不会添加到数组中。

欢迎并赞赏任何帮助

I'm trying to parse a string into smaller ones, extracting some values and then I want to check if any of these values is a dupe...

Here's my lame code :)

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="INVITE sip:[email protected] SIP/2.0\nCall-ID: [email protected] To: <sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;";
  char * tch;
  char * saved;  
char * array[50];
int count = 0;        
  tch = strtok (str,"<:;>");
    while (tch != NULL)
  { 
    int savenext = 0;              
    if (!strcmp(tch, "sip"))   
    {                             
      savenext = 1;                
    }                               
    printf ("%s\n",tch);
    tch = strtok (NULL, "<:;>");
    if (savenext == 1)             
    {                              
      saved = tch;                  
    }                              

if ( count == 0 ) {
    array[count] = saved;  
    count ++;  
    }
    if ( count > 0 ) {
        int i = 0;
        while (i < count ) {
            if (array[count] == saved ) {
                printf("FOUND!");
                }
                i++;}
                }


            }
 }

What I have to do is check if the same username is found twice in the string, but my lack of experience with pointers prevents me from this. I can't figure out why the values won't be added to the array.

Any help is welcome and appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-11-21 02:22:48

您已完成

if ( count == 0 ) {
array[count] = saved;  
count ++;  
}

这意味着您将saved的地址保存到array[count]中。在此操作中,没有复制任何字符串。

然后您可以执行以下操作:

if (array[count] == saved ) {
  printf("FOUND!");
}

上面将 array[count] 中存储的值与 saved 中存储的地址进行比较。此操作不会比较其中存储的字符串。

因此,如果array[count]中的地址0x1234abcd指向字符串“alice”,并且saved指向存储在另一个中的字符串“alice”内存位置 0xdeadbeefarray[count] == string 与本例中的 0x1234abcd == 0xdeadbeef 不同正在完成。要比较两个字符串,您需要执行 strcmp (array[count], saving) == 0

请注意,

    while (i < count ) {
        if (array[count] == saved ) {
            printf("FOUND!");
            }
      i++;
     }

在上面的代码中,您增加了 i ,但使用 count 访问了 array ,它在一次传递中是静态的,并且不取决于i。应该是 array[i]

你已经完成了

if (count == 0)
{
   array[count] = saved;  
   count ++
}
if (count > 0)
{
  /* Here you try to search if the 'saved' is in the aray
     but if it is not there you have NOT inserted it anywhere 
     into the array
   */
}

因为你在 count > > 时没有输入 saved 指向的字符串。 0,因此除了第一个字符串之外的唯一字符串都存储在数组中。因此,只要您发现新的字符串不在 if (count > 0) 块中的字符串中,就应该将其保存到数组中。如下段所述:

  if (count > 0)
    {
      int i = 0;
      while (i < count)
        {
          /* note use of strcmp */
          if (strcmp (array[i], saved) == 0)
            {
              printf ("FOUND!"); /* if it was found break immediately */
              break;
            }
          i++;
        }
       if (i == count) /* if there was no match then only i == count  */
        {              /* in other cases when dupes are there i<count as we used break */
          array[count] = saved;
          count++;
        }

    }

这是修改后的代码,反映了上述更改。

#include <stdio.h>
#include <string.h>
int main (void)
{
  char str[] =
    "INVITE sip:[email protected] SIP/2.0\nCall-ID: [email protected] To: <sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;";
  char *tch;
  char *saved;
  char *array[50];
  int count = 0, i;

  tch = strtok (str, "<:;>");
  while (tch != NULL)
  {
      int savenext = 0;
      if (!strcmp (tch, "sip"))
  {
      savenext = 1;
      }
     // printf ("%s\n", tch);
     tch = strtok (NULL, "<:;>");
     if (savenext == 1)
 {
   saved = tch;
 }

    if (count == 0)
    {
  array[count] = saved;
  count++;
    }
    else if ((count > 0) && (savenext == 1))
    {
      int i = 0;
      while (i < count)
      {
        if (strcmp (array[i], saved) == 0)
    {
      printf ("FOUND!");
      break;
    }
        i++;
      }
      if (i == count)
      {
        array[count] = saved;
        count++;
      } 

    }
     }

    for (i = 0; i < count; i++)
       printf ("\n%s", array[i]);
}

编辑1:

回答您的评论:
假设 strtok 匹配了 "sip" ,那么它会让 saveptr = 1 读取下一个和 tch 中的令牌,即用户名信息并在 saveptr 的帮助下将其保存到 array 中。在下一次迭代中,请注意 tch 指向存储在数组中的用户名信息。因此 strcmp 失败,因为它不是“sip”(包含用户名信息)。因此,在这种情况下,保存的虽然没有被修改,但它仍然保留以前的值,该值再次进入if (count > 0)块。因此,在您的流程中,一个用户信息会被检查两次。你应该

if ((count > 0) && (savenext == 1))
{
     /* Then insert */
}

按照上面的代码所说,如果 saveptr == 那么 saved 需要保存在 array 中,这就是为什么你接受了 saveext 标志。

我也更新了代码。现在它正确地表明只有一个重复项。

我建议重新设计代码并使其更加简洁。您可能想再次详细了解指针以使其更好。

You have done

if ( count == 0 ) {
array[count] = saved;  
count ++;  
}

Which means that you save the address of the saved into array[count]. In this operation no strings were copied.

Then you do:

if (array[count] == saved ) {
  printf("FOUND!");
}

The above compares the value stored in array[count] with the address stored in saved. This operation does not compare the strings stored in them.

So if an address 0x1234abcd in array[count] points to a string "alice" and saved points to the string "alice" stored in another memory location 0xdeadbeef then array[count] == string would not be same as in this case 0x1234abcd == 0xdeadbeef is being done. To compare the two strings you need to do strcmp (array[count], saved) == 0 .

Note that you do

    while (i < count ) {
        if (array[count] == saved ) {
            printf("FOUND!");
            }
      i++;
     }

In the above code, you have incremented i but accessed the array with count which is static for one pass, and does not depend on i. It should be array[i]

You have done

if (count == 0)
{
   array[count] = saved;  
   count ++
}
if (count > 0)
{
  /* Here you try to search if the 'saved' is in the aray
     but if it is not there you have NOT inserted it anywhere 
     into the array
   */
}

Because you do not enter the string pointed by saved when count > 0 so the unique strings except the first one does not gets stored in the array. So you should save the new sting into the array whenever you find that it is not in the sting in the if (count > 0) block. Like as described in the following segment:

  if (count > 0)
    {
      int i = 0;
      while (i < count)
        {
          /* note use of strcmp */
          if (strcmp (array[i], saved) == 0)
            {
              printf ("FOUND!"); /* if it was found break immediately */
              break;
            }
          i++;
        }
       if (i == count) /* if there was no match then only i == count  */
        {              /* in other cases when dupes are there i<count as we used break */
          array[count] = saved;
          count++;
        }

    }

Here is the modified code, which reflect the above changes.

#include <stdio.h>
#include <string.h>
int main (void)
{
  char str[] =
    "INVITE sip:[email protected] SIP/2.0\nCall-ID: [email protected] To: <sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;<sip:[email protected]>;";
  char *tch;
  char *saved;
  char *array[50];
  int count = 0, i;

  tch = strtok (str, "<:;>");
  while (tch != NULL)
  {
      int savenext = 0;
      if (!strcmp (tch, "sip"))
  {
      savenext = 1;
      }
     // printf ("%s\n", tch);
     tch = strtok (NULL, "<:;>");
     if (savenext == 1)
 {
   saved = tch;
 }

    if (count == 0)
    {
  array[count] = saved;
  count++;
    }
    else if ((count > 0) && (savenext == 1))
    {
      int i = 0;
      while (i < count)
      {
        if (strcmp (array[i], saved) == 0)
    {
      printf ("FOUND!");
      break;
    }
        i++;
      }
      if (i == count)
      {
        array[count] = saved;
        count++;
      } 

    }
     }

    for (i = 0; i < count; i++)
       printf ("\n%s", array[i]);
}

EDIT1:

Answer to your comment:
Say the strtok has matched "sip" , then it makes saveptr = 1 reads in the next and token in tch , ie, the username info and saves it into array with the help of saveptr. In the next iteration note that tch points to the username info that was stored in the array. So the strcmp fails as it is not "sip" (contains the user name info). So in this case the saved is although not modified it still holds the previous value, which enters the if (count > 0) block again. So one user information is checked twice in your process. You should do

if ((count > 0) && (savenext == 1))
{
     /* Then insert */
}

What the above code says that, if saveptr == then the saved needs to be saved in the array, that is why you took the flag savenext.

I have updated the code too. Now it correctly tells that there is only one duplicate.

I would recommend to redesign the code and make it a bit more clean. Probably you would like to have another look about the pointers in details to make it better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文