[C] filter a buffer, and write to new char

ilovefrits's Avatar, Join Date: Sep 2009
Newbie Member
Hello,

Im working on the following code in C, which has to read a char, then filter the symbol - from the char, and write it back without the - symbol to a new char.
But im currently running into some compile errors, I've been looking around how to work with memset and strcat but still cannot figure out what is going wrong.

Please help me out
Code:
char neww[255];
memset(neww, 0, 255);
for(int i = 0; i<strlen(pid+1); i++)
         { if(pid[i] != "-");
           strcat(neww, pid[i]);
}


where the char pid contains a value like 3434-111-12222-222.
Any suggestions, help ? thank you in advance
ilovefrits's Avatar, Join Date: Sep 2009
Newbie Member
Im sorry, couldn't find the edit button here are the compile errors, and where they occur.

Error: '!=' : no conversion from 'char *' to 'int'
Error: '!=' : 'int' differs in levels of indirection from 'char [2]'
Code:
{ if(pid[i] != "-");
Error: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
Code:
  strcat(newpid, pid[i]);

Thanks in advance,
vishnukumar's Avatar, Join Date: Sep 2009
Newbie Member
Can you post the full code?
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
strcat takes two strings, it does not work on a string and a char.
So strcat(str,"A"); is valid, but strcat(str,'A'); is not, because 'A' is a single char not a string.
This is why you get the error.
A better solution is to use two index variables, one that indexes the current character in pid, and one that indexes the current character in neww.
If the character is one you want, copy it and increment both indices (indices=plural of index).
If the character is not one you want, just increment the index into pid.
Don't forget to add a null terminator when you're done.
ilovefrits's Avatar, Join Date: Sep 2009
Newbie Member
Thank you for the advice xpi0t0s, I've managed to get the code working.
I've made a void to process the parsing, because not only one char had to be done but more. Also didn't use the strcat function but managed to do without, also added a null to the char at the end.

Works fine thank you again for the help/advice. Close ?