I’m trying to program a code in C++ for my assignment.
What happen is one of the part where I have to accept some data from my main and in my function I have to get first part of the array that sent by main and put in my array in the function.
for (int i = 0; i <= strlen(main) && exit == 0; i++){
if (main[i] != ';' || main[i] != '\0'){
keyword[i] = data[i];
if(main[i] == ';' || main[i] == '\0')
exit = 1;
}
This is the code in the array named main = "Hello World;Yes;No;Okay;Good Bye",
So what happen is I want to store that Hello World in my array called keyword and the problem is once I printf the keyword string I see extra data after the word Hello World
Here is what I have on the printf
Your keyword-----> 'Hello World;? '
Actual keyword---> 'Hello World'
Is there any problem with my logic use above??
Thanks
i <= strlen(main)needs toi < strlen(main)and there is no need for the extra termination character check in theif. Also make sure thatkeywordis null terminated (\0) after copying the necessary data.Just to get you idea, assuming the destination is big enough to hold the data being copied.