/*the program gots a string, and supposed to copy to the destination
string every letter only once, and after a letter a num - a counter how many times a letter appeared in the original string
for example - for kuku it should return k2u2 - but it returns k0u0 - can someone help??? */


#include <stdio.h>
int RLE (char *string, char letter);
void Change(char *source, char *dest);

void main()

{

char str[20];
char enc[20];
printf("enter a string...");

gets_s(str);
Change(str, enc);
printf("%s",enc);
//puts(enc);


}




void Change(char *source, char *dest)
{

char *s;
char *d;
int i;
d=dest;
s=source;



for (i=0;*s;i++,s++)
{
if(RLE (d, s[i])==0) //meaning if in the destination string hasn't already the letter
{
*d=*s; //then add the letter
*(++d)=(RLE(source, s[i]))+'0'; //this one adds how many times the letter appears in the source string
d++; 
}



}
*d='\0';
}

int RLE (char *string, char letter) //returns count of letter's appearance in string
{
char *b;
int count=0;
b=string;

while(*b)
{ if(*b==letter)
count++; //why the count stays on 0???
b++;
}

return count;
}
/*yes, you're right, this is one of my problems. here is a revisited version, but still it doesn't work as i want it. if i input hihi
now it gives h2i2h2i2h2i2 instead of h2i2 */
                                                                    
                                                                       
 #include <stdio.h>
int RLE (char *string, char letter);
void Change(char *source, char *dest);

void main()

{

char str[20]="";
char enc[20]=""; 
printf("enter a string...");

gets(str);
Change(str, enc);
printf("%s",enc);
puts(enc);


}




void Change(char *source, char *dest)
{

char *s;
char *d;
int i;
d=dest;
s=source;


for (i=0;s[i];i++)  
{
if(RLE (dest, s[i])==0) 
{
*d=s[i]; 
*(++d)=(RLE(source, s[i]))+'0'; 
d++;
*d='\0'; 
}
}
}

int RLE (char *string, char letter) //returns count of letter's appearance in string
{
char *b;
int count=0;
b=string;

while(*b)
{ if(*b==letter)
count++; 
b++;
}

return count;
}

Edited 6 Years Ago by yila: n/a

You still failed to declare those two arrays as I showed you. Also problems in Change() function. main() returns an int, not void.

int main()

{

    char str[20]= {0};
    char enc[20] = {0}; 
printf("enter a string...");

gets_s(str);
Change(str, enc);
printf("%s",enc);


}




void Change(char *source, char *dest)
{

char *s;
char *d;
int i;
d=dest;
s=source;


for (i=0;source[i];i++)  
{
if(RLE (dest, source[i])==0) 
{
*d=source[i]; 
*(++d)=(RLE(source, source[i]))+'0'; 
d++;
}
}
}

Edited 6 Years Ago by Ancient Dragon: n/a

a classmate helped me out, so here is the solution if someone is interested. and thanx a lot, AncientDragon, your suggestions were also very helpful.

void Change(char *source, char *dest)
{
	char *s;
	char *d;
	int i;
	d=dest;
	s=source;
	
	for (i=0;s[i];i++) 
	{
		if(!RLE(dest, s[i])) 
		  {
			*d=s[i]; 
			 *(++d)=(RLE(source, s[i]))+'0'; 
			d++;
		  }
	  }
	*(++d)='\0';
}
This question has already been answered. Start a new discussion instead.