byte id=0;
char a[100]={0};
char c=0;
for(byte i=0;i<100 && serial.available()>0;i++)
c=serial.read();
a[i]=c;
delaymicroseconds(86);
}
//now i want to add id which is 0 to the string
byte _length=strlen(a); //length=11
a[_length+1]=id;
a[_length+2]=‘\0’;
_length=strlen(a);
// and it shows same length as without adding id i.e 11.
How to add id?
While it pains me to ignore the obvious mistake and the likely other mistakes in your snippet I will directly address your question...
a[_length+0]=id;
a[_length+1]='\0';
_length=strlen(a);
You will have to convert the id to text. The number 0 is the same as the terminating null character. If your id is limited to 0..9, it's easy (look at the ascii table); else look at the itoa function.
I want to send string+id via serial.
Is serial.print() adding something at the end of the string?
Like serial.print(string);serial.print(id-48);
Is same as serial.print(stringwithid)? For the receiver side?
_length=strlen(a);
Serial.println(_length); //prints 11
a[_length]=id;
a[_length+1]='\0';
_length=strlen(a);
Serial.println(_length); //prints 11
Serial.println(id);
Serial.println(a);
byte id has value from atoi conversion done manually (x-48); id is holding numbers from 0 to 4.
Serial.print(a);
Serial.println(id); solves the problem but I cant understand how to add a variable to string.
You declared your string as 100 characters, so that is what strlen will return.
You should only fill that up to 99 chars in your for loop since the last element should be a null character.
If you want to add an Id to then only fill it up to 98 characters in your for loop.
surepic:
byte id has value from atoi conversion done manually (x-48); id is holding numbers from 0 to 4.
Then a reasonable way to reverse that operation is to add 48...
a[_length+0]= id + 48;
a[_length+1]='\0';
_length=strlen(a);
But, a better choice is to eliminate the magic number...
a[_length+0]= id + '0';
a[_length+1]='\0';
_length=strlen(a);
@Coding Badly thanks for explanation now I got it!
noweare:
my bad
No offense intended; do you know what one should use to get the size of the array?
The 'sizeof' the array or the number of elements in the array?
(int)sizeof(array)/(sizeof(array[0]))
lloyddean:
The 'sizeof' the array or the number of elements in the array?
sizeof gives the size in bytes. surepic's solution gives the number of elements (for char or byte, the result is the same).
surepic:
(int)sizeof(array)/(sizeof(array[0]))
The cast causes a failure if the array size overflows a positive signed integer (32767 bytes for an AVR Arduino).
When working with sizeof don't cast. If you do need to cast do it to an unsigned type.
noweare:
Why are you asking me ?
Because you gave the wrong answer 
sterretje:
Because you gave the wrong answer 
And you said "No offense"... You ever make a mistake genius ?