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:alice1@open-ims.test SIP/2.0\nCall-ID: mndfdnf8e4953t984egnue@open-ims.test To: <sip:alice2@open-ims.test>;<sip:alice@open-ims.test>;<sip:alice4@open-ims.test>;<sip:alice5@open-ims.test>;<sip:alice6@open-ims.test>;<sip:alice@open-ims.test>;<sip:alice8@open-ims.test>;<sip:alice9@open-ims.test>;<sip:alice10@open-ims.test>;<sip:alice11@open-ims.test>;<sip:alice12@open-ims.test>;";
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
You have done
Which means that you save the address of the
savedintoarray[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
0x1234abcdinarray[count]points to a string “alice” andsavedpoints to the string “alice” stored in another memory location0xdeadbeefthenarray[count] == stringwould not be same as in this case0x1234abcd == 0xdeadbeefis 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
ibut accessed thearraywithcountwhich 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
savedwhencount > 0so 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
strtokhas matched “sip” , then it makessaveptr = 1reads in the next and token intch, ie, the username info and saves it intoarraywith the help ofsaveptr. In the next iteration note thattchpoints to the username info that was stored in the array. So thestrcmpfails as it is not “sip” (contains the user name info). So in this case thesavedis 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 thesavedneeds 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.