I'm trying to make a RLE (Run-Length Encoder) Programme just for characters. I read the way it works on notes on net. And I tried to fix my code! Regardless I think that the steps of code are right, the code doesnt work! It appears some strange 'Z' as it runs. I really can't find what;s wrong! Could you please give me a piece of advice?
#include <stdio.h>
int main()
{
int count;
unsigned char currChar,prevChar=EOF;
while(currChar=getchar() != EOF)
{
if ( ( (currChar='A')&&(currChar='Z') ) || ( (currChar='a')&&(currChar='z') ) )
{
printf("%c",currChar);
if(prevChar==currChar)
{
count=0;
currChar=getchar();
while(currChar!=EOF)
{
if (currChar==prevChar)
count++;
else
{
if(count<=9)
printf("%d%c",count,prevChar);
else
{
printf("%d%c",reverse(count),prevChar);
}
prevChar=currChar;
break;
}
}
}
else
prevChar=currChar;
if(currChar==EOF)
{ printf("%d",count);
break;
}
}
else
{
printf("Error Message:Only characters are accepted! Please try again! False input!");
break;
}
}
return 0;
}
int reverse(int x)
{
int p,y,r=0;
x=(x<0)?-x:x;
while (x>0)
{
y=x%10;
p=x/10;
r=10*r+y;
x=p;
}
printf("%d",r);
return 1;
}
e.g. I gave the input:
AAAAAAAAAAAAAAAAAAAAFFFFFFFFFFFFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEEEEEEEEEEEGGGGGGGGGGGGGGGGVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSRRRRRRRRRRHD RRRRRRRRRRRRRRRRRRRRHHHHHHHHHHHHHHHHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYNNNNNNNNNNNNNNNNNNNNNNNNNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMS TTTTTTTTTTHHHHHHHHHHHH
and I got the output:
Z0AZZ0AZZ0FZZ0FZZ0FZZ0FZZ0FZZ0FZZ0FZZ0FZZ0FZZ0EZZ0EZZ0EZZ0EZZ0EZZ0EZZ0EZZ0EZZ0GZZ0GZZ0GZZ0GZZ0GZZ0GZZ0VZZ0VZZ0VZZ0VZZ0VZZ0VZZ0VZZ0VZZ0VZZ0VZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0SZZ0RZZ0RZZ0RZZ0RZZ0 ZZ0RZZ0RZZ0RZZ0RZZ0RZZ0RZZ0HZZ0HZZ0HZZ0HZZ0HZZ0HZZ0AZZ0AZZ0AZZ0AZZ0AZZ0AZZ0AZZ0AZZ0AZZ0AZZ0
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :(