/*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;
}

The problem is that you did not clear out the destination buffer when you declared it

char str[20] = {0};
char enc[20] = {0};
/*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;
}

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++;
}
}
}

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';
}
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.