So today I was messing around with some string parsing in C and I started receiving an odd string formatting. I believe it has something to do with the strcat between the array "functionName" and the pointer "fiterator", but I cant figure out how to fix it:
I'm trying to print "test()" but rather i'm receiving test()est()st()t()
#include <stdio.h>
#include <string.h>
void __INTERFunc(char args[])
{
char * argsp;
char functionName[25] = {};
for (argsp = args; *argsp != '\0'; argsp++)
{
if (*argsp == '(')
{
char *fiterator;
for (fiterator = args; *fiterator != '('; fiterator++)
{
strcat(functionName, fiterator);
}
}
}
char * nmiter = functionName;
while (*nmiter != NULL)
{
printf("%c", *nmiter);
nmiter++;
}
// This block is for testing purposes
// Still receiving output: test()est()st()t()
printf("\n");
printf("%s\n", functionName);
}
int main()
{
__INTERFunc("test()");
}
testortest()? Anyway, I think the strcat is the source of the strange behavior but its hard to give an answer without knowing exactly what you want. The best thing I can suggest is that you should fill your programs with printf statements showing the state of the functionName buffer throughout the execution of the algorithm. That should clear things up a lot.test()but yeah I'll definitely try the printf statmentstest()but what is thetest()supposed to mean? Why can't you justprintf("%s", args)and be done with it?