Reputation: 781
This is the code I am executing:
scanf("%s",expr);
i=0,j=0;
while(expr[i]!='+')
{
l[j++]=expr[i++];
}
j=0;
while(expr[i]!='=')
{
r[j++]=expr[i++];
}
j=0;
while(expr[i]!='\0')
{
s[j++]=expr[i++];
}
printf("%s %d %s %d %s %d",l,strlen(l),r,strlen(r),s,strlen(s));
I cannot understand why this is not showing proper output.
For e.g. in case of 1+1=2
Output should have been 1 1 +1 2 =2 2
But what i am getting is 1 2symbols 3 +1 2 =2 3symbols 5
Upvotes: 1
Views: 131
Reputation: 5369
Add \0
at the end of each string.
scanf("%s",expr);
i=0,j=0;
while(expr[i]!='+')
{
l[j++]=expr[i++];
}
l[j]='\0'; //here
j=0;
while(expr[i]!='=')
{
r[j++]=expr[i++];
}
r[j]='\0'; //here
j=0;
while(expr[i]!='\0')
{
s[j++]=expr[i++];
}
s[j]='\0'; //and here
printf("%s %d %s %d %s %d",l,strlen(l),r,strlen(r),s,strlen(s));
Upvotes: 1