Start New Discussion within our Software Development Community

I wrote a program for counting and printing characters in a data link layer frames i've succeeded in getting output for the first frame but for the other frames it showed not valid please help in the code

If i give an input as 6sudhi4div then the integer in the input is the size of frame and the characters after the integer are the contents of the frame including the size of the frame( for example size 6 contents : 6sudhi)

The code is

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<numver.h>
#include<stdlib.h>

#define N 644

void main()
{
	int i,l,c,f,m;
	char s[N];

	i=l=c=f=m=0;

	printf("Enter the dataframe");
	scanf("%s",s);

	l=strlen(s);


	while(l!=0)
	{

		if(isnum(s[i])==1)
			{
				f++;
				c=i+s[i]-1;
				for(m=i+1;m<=c;m++)
				{
					if(isnum(s[m])==1)
						{
							printf("Its an invalid dataframe");
							getch();
							exit(0);
						}
				  else
				  {
					printf("Frame %d contains following charecter",f);
					printf("\t %c",s[m]);
					l--;
				}
		  }
		}
		i=i+s[i];
  }
getch();
}

Thanks

Comments
Thanks for using code tags on your first post :)

There are many mistakes in your code

1. When you wanted to remove the number 6, you took the ASCII value of 6 ie 54 but forgot to subtract the ASCII value of 0. Hence your i variable became too big.
2. When you read the number 6 you have only 5 characters to read. So the for loop should not be <= but just <
3. When you start the second iteration, the value of c is 4 but the value of m is already greater than 4 so it does not enter the loop
4. Ensure that after you have finished reading the frame , the value of has decremented upto the correct value
5. After you have made all these changes , remove scanf and use fgets instead

This article has been dead for over six months. Start a new discussion instead.