--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