--Homework--
Write a C program that codes a string which is given by keyboard so that the coded string includes
characters with their number of sequential occurrences. For example, if the string to be coded is
AAABBFFFRRRRZTT then the coded string must be A3B2F3R4Z1T2. You can assume that there is no
reoccurrence of a character more than 9. You have to use pointers. Note: The encoded characters
should not be printed to the screen directly; they should be written into a string, then that string
must be printed to the screen.

I have worked on it, and here is the code i wrote

#include <stdio.h>
#include <string.h>

main()
{
	int i,j,k;
	char ch[150],*c[150],ch2[150],*c2[150],counter; //we defined the variables
	for (i=0;i<150;i++) c[i]=&ch[i];
	for (i=0;i<150;i++) c2[i]=&ch2[i];//assigning the variables to pointers
	for (i=0;i<150;i++) *c2[i]=0; //cleared the pointer
	printf("Enter the string:");
	gets(*c); //scanned on first pointer
	k=-1; //this k integer works as a counter for 2. string
	for(i=0;i<99;i++)
	{
		k++;
		*c2[k]=*c[i]; //here i copied first element of the c to c2
		counter=1; //this is the counter which counts the repetations 
		for(j=0;j<10;j++)
		{
			if(*c[i+j]!=*c[i+j+1]) //if the element is not equal to the next element, loop breaks
			{	
				break;
			}
			if(*c[i+j]==*c[i+j+1]) counter++; //if the element is equal to the next element, counter++
		}
		i++;
		*c2[k+1]=counter+49; //here it writes down the counter to c2 string
		i+=j;
		k++;


	}
	puts(*c2); //writes down the c2 string





	getch();
}

And here is the problems:
1- The counter fails for the first character. For example if i write AAAAAaaaBB it writes; A6a3B2, but it should A5a3B2.
2-The counter also fails for 1 char lengths. If i write AaBbCc, it gives me A2B2C2, but it should give me A1a1B1b1C1c1.
3-I try to clear the pointer c2's string at start, but it writes stupid characters after the string. Example; "A1B1C1|||||||||||"

Thanks

Wow you don't have to do all that

I don't have a C compiler I can't get any to work on linux, nvm about that.

I am gonna give you the pseudo code u have to do the work.

Parse the string with substr.

Basically

int counter=0;
string newstr="";

for (int i=1; i< string.size;i++)
{
  if (string.substr(i-1,1).compare(i,1)==0) 
 {
   counter++;
 }
else 
{
 newstr =newstr + string.substr(i,1) + counter;
 counter =0;

}

}

ok I just read your question again, You need to start counting from index 1 and look back 1 every time. what you are doing is checking it with the char infornt. By looking at the above code it should make sense with what you are doing.

thanks for your help but i couldn't figure out how to imply "string.substr(i-1,1).compare(i,1)==0" this in C

Solved it :D I am pasting code here, maybe helps to someone

#include <stdio.h>
#include <string.h>


main()
{
	int i,j,k,check;
	char ch[150],*c[150],ch2[150],*c2[150],counter; //we defined the variables
	for (i=0;i<150;i++) c[i]=&ch[i];
	for (i=0;i<150;i++) c2[i]=&ch2[i];//assigning the pointers to variables
	for (i=0;i<150;i++) *c2[i]=0; //cleared the pointer's variable
	printf("Enter the string:");
	gets(*c); //scanned to first pointer
	k=-1; //this k integer works as a counter for 2. string
	for(i=0;i<99;i++)
	{
		if(*c[i]==0) break; //to break the loop after string finishes
		k++;
		*c2[k]=*c[i]; //here i copied first element of the c to c2
		counter=1; //this is the counter which counts the repetations 
		for(j=0;j<10;j++)
		{
			if(*c[i+j]!=*c[i+j+1]) break;//if the element is not equal to the next element, loop breaks
			if(*c[i+j]==*c[i+j+1]) counter++; //if the element is equal to the next element, counter++
		}
		i+=j;
		k++;
		*c2[k]=counter+48; //here it writes down the counter to c2 string
		
		


	}
	puts(*c2); //writes down the c2 string





	getch();
}

thanks for the link :D

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.