Hey, Some one Please help in Completing this code of CRC

char* substr(char* pra,int strt,int end)//To Evaluate Substring
{
  char pr[5]="\0";
  int j,k;
  for(j=strt,k=0;j<=end&&k<5;j++,k++)
    pr[k]=pra[j];
  return pr;
}
char* exor(char* pra,char* div)//To Evaluate Mod2 Operation
{
  char out[5]="\0";
  int i;
  for(i=0;i<strlen(pra);i++)
  {
    if(pra[i]==div[i])
     out[i]='0';
    else
     out[i]='1';
  }
  return out;
}
void main()
{
  char *gen,*fra="\0",*temp="\0",*quo="\0",*x;
  int i,j,flag,cntr=0;
  clrscr();
  printf("Enter the Frame");
  scanf("%s",fra);
  printf("Enter the Generator");
  scanf("%s",gen);
  for(i=1;i<strlen(gen);i++)
  strcat(fra,"0");
  for(i=0;i<strlen(fra)-4;i++)
  {
     x=substr(fra,i,i+4);
     temp=exor(substr(fra,i,i+4),gen);
     if(temp[0]=='0'&&temp[1]!='0')
       strcat(quo,'1');
     else
     {
       flag=0;
       for(j=0;j<strlen(quo);j++)
       {
      if(temp[i]=='0'&&flag==0)
        cntr++;
      else{
        flag=1;
        break;
      }
       }
       for(j=0;j<cntr;j++)
     strcat(quo,'0');
       i=i+cntr;
     }

  }
  printf("%s",quo);
}

Recommended Answers

All 12 Replies

To start,
These char pointers:

char *gen,*fra="\0",*temp="\0",*quo="\0",*x;

need to be declare as array of strings id est,

char gen[SIZE] = { '\0' };

Because you intent to use them as variable strings and that will not work.
They are constants so you can not change the value in them, you can only make them to point to something else.

In functions you are returning pointers of local variables.
exempli gratia

char* substr(char* pra,int strt,int end)
{
    char pr[5]="\0";
    <snip>
    return pr;
}

That variable will disappear when the function is done. Not pointer can be pass to the call.

Aia, The substr method is workin' but the problem is that
in the line 35x value is Correct only but when it is give as parameter to exor it is taking as NULL pls give me the Corrected Code Completely(First Trace it out u can know the Problem)

>The substr method is workin'
No, it's not. substr returns a local array. When the function returns, the array gets destroyed, and the return value becomes garbage. This is a very basic problem that you should be aware of.

>pls give me the Corrected Code Completely
We're not here to do your work. You've been told what's wrong, and if you can't fix it on your own, you're trying to write something that's too far beyond your ability.

"Writing somethin beyond one's ability" Thats not fair, help the dude not just discourage him. Otherswise whats the use of this website if the whole perception about things is negative. The guy has tried regardless how wrong he is, Try weighing the words you type before you click the enter key. Thats on a light note. Thanks!

>Thats not fair, help the dude not just discourage him.
He's already wasted the help that was given. And he was told exactly what was wrong and how to fix it. If he can't follow simple instruction or figure out how to fix his problem given the solution, he's programming beyond his ability. It might not be fair, but that's how it is.

>Otherswise whats the use of this website if the whole perception about things is negative.
We're not a homework service. If you ask for us to write your code, or fix your code, or troubleshoot your code and tell you where the bugs are, that defeats the purpose of this forum. We're not here to make you feel good about yourself, we're here to help you learn to program. If we do everything for you, all you learn is how to mooch off of others.

>Try weighing the words you type before you click the enter key
Just because you disagree with my choice of words doesn't mean I haven't considered them carefully.

naure, I have Corrected the Code n now the exor method is not workin' even i have changed the exor function I have declared the Output variables Globally Am I Right in this way?

Am Giving u The Code Mr.Naure

char pr[5],out[5];
void substr(char* pra,int strt,int end)
{
  int j,k;
  strcpy(pr,"\0");
  for(j=strt,k=0;j<=end&&k<5;j++,k++)
    pr[k]=pra[j];
}

void exor(char* pra,char* div)
{
  int i;
  strcpy(out,"\0");
  for(i=0;i<strlen(pra);i++)
  {
    if(pra[i]==div[i])
     out[i]='0';
    else
     out[i]='1';
  }
}
void main()
{
  char *gen,*fra="\0",*quo="\0",*x;
  int i,j,flag,cntr=0,k;
  clrscr();
  printf("Enter the Frame");
  scanf("%s",fra);
  printf("Enter the Generator");
  scanf("%s",gen);
  for(i=1;i<strlen(gen);i++)
  strcat(fra,"0");
  for(i=0;i<strlen(fra)-4;i++)
  {
     substr(fra,i,i+4);
     printf("%s",pr);
     exor(pr,gen);
     if(out[0]=='0'&&out[1]!='0')
	strcat(quo,'1');
     else
     {
       flag=0;
       cntr=0;
       k=0;
       for(j=0;j<strlen(quo);j++)
       {
	 for(k=0;k<strlen(out);k++)
	 {
	  if(out[k]=='0'&&flag==0)
	    cntr++;
	  else{
	    flag=1;
	    break;
	  }
	 }
       }
       for(j=0;j<cntr;j++)
	 strcat(quo,'0');
       i=i+cntr;
     }

  }
  printf("%s",quo);
}

Am Giving u The Code Mr.Naure

Sorry couldn't help comment off topic..
It's Ms. Naure not Mr. :D
See the avatar?

See the avatar?

Do you look like your avatar?.

> Do you look like your avatar?.
He meant gender. And yes, I do look like my avatar. :-)

> Do you look like your avatar?.
He meant gender. And yes, I do look like my avatar. :-)

I don't look like mine --- much... :-)

I don't look like mine --- much... :-)

I was wondering today about that. You're too little now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.