C 在数组中添加和搜索解析数据
我正在尝试将字符串解析为较小的字符串,提取一些值,然后我想检查这些值中的任何一个是否是重复的...
这是我蹩脚的代码:)
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已完成
这意味着您将
saved
的地址保存到array[count]
中。在此操作中,没有复制任何字符串。然后您可以执行以下操作:
上面将
array[count]
中存储的值与saved
中存储的地址进行比较。此操作不会比较其中存储的字符串。因此,如果
array[count]
中的地址0x1234abcd
指向字符串“alice”,并且saved
指向存储在另一个中的字符串“alice”内存位置0xdeadbeef
则array[count] == string
与本例中的0x1234abcd == 0xdeadbeef
不同正在完成。要比较两个字符串,您需要执行strcmp (array[count], saving) == 0
。请注意,
在上面的代码中,您增加了
i
,但使用count
访问了array
,它在一次传递中是静态的,并且不取决于i
。应该是array[i]
你已经完成了
因为你在
count > > 时没有输入
,因此除了第一个字符串之外的唯一字符串不都存储在saved
指向的字符串。 0数组
中。因此,只要您发现新的字符串不在if (count > 0)
块中的字符串中,就应该将其保存到数组中。如下段所述:这是修改后的代码,反映了上述更改。
编辑1:
回答您的评论:
假设
strtok
匹配了 "sip" ,那么它会让saveptr = 1
读取下一个和tch
中的令牌,即用户名信息并在saveptr
的帮助下将其保存到array
中。在下一次迭代中,请注意tch
指向存储在数组中的用户名信息。因此strcmp
失败,因为它不是“sip”(包含用户名信息)。因此,在这种情况下,保存的
虽然没有被修改,但它仍然保留以前的值,该值再次进入if (count > 0)
块。因此,在您的流程中,一个用户信息会被检查两次。你应该按照上面的代码所说,如果
saveptr ==
那么saved
需要保存在array
中,这就是为什么你接受了saveext
标志。我也更新了代码。现在它正确地表明只有一个重复项。
我建议重新设计代码并使其更加简洁。您可能想再次详细了解指针以使其更好。
You have done
Which means that you save the address of the
saved
intoarray[count]
. In this operation no strings were copied.Then you do:
The above compares the value stored in
array[count]
with the address stored insaved
. This operation does not compare the strings stored in them.So if an address
0x1234abcd
inarray[count]
points to a string "alice" andsaved
points to the string "alice" stored in another memory location0xdeadbeef
thenarray[count] == string
would not be same as in this case0x1234abcd == 0xdeadbeef
is being done. To compare the two strings you need to dostrcmp (array[count], saved) == 0
.Note that you do
In the above code, you have incremented
i
but accessed thearray
withcount
which is static for one pass, and does not depend oni
. It should bearray[i]
You have done
Because you do not enter the string pointed by
saved
whencount > 0
so the unique strings except the first one does not gets stored in thearray
. So you should save the new sting into the array whenever you find that it is not in the sting in theif (count > 0)
block. Like as described in the following segment:Here is the modified code, which reflect the above changes.
EDIT1:
Answer to your comment:
Say the
strtok
has matched "sip" , then it makessaveptr = 1
reads in the next and token intch
, ie, the username info and saves it intoarray
with the help ofsaveptr
. In the next iteration note thattch
points to the username info that was stored in the array. So thestrcmp
fails as it is not "sip" (contains the user name info). So in this case thesaved
is although not modified it still holds the previous value, which enters theif (count > 0)
block again. So one user information is checked twice in your process. You should doWhat the above code says that, if
saveptr ==
then thesaved
needs to be saved in thearray
, that is why you took the flagsavenext
.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.