Hello,
i need to do a program which takes an input sequence of characters from the alphabet {A,B,C} and compresses it by replacing each subsequence of a particular character by a single occurrence of the character, and the length of the substring.
e.g. Input : AAAAABBBBBBBCCBBBAABBCCCCCS
Output: A5B7C2B3A2B2C5

i dont even know how to start any help please.

i know how to count characters of a user input without an array
here it is:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  
  char ch;
  int i=0;
  
  printf("Enter a series of characters\n");
  
  do
  {
      i++;
      scanf("%c", &ch);
      }
      while(ch!='.');
   
  i--;
  
  printf("Number of characters= %d\n", i);
  
  system("PAUSE");	
  return 0;
}

any help please
thanks

Do you know what is a char array ?

u mean something like this:

char array[100]

??

Yes take i/p in the char array instead of the string. The for the algorithm you want something of this sort

char arr[]= "input string"
char a= arr[0]
int count =1

loop i from 1 to the length of the string
if arr is equal to a then increase count
else
{
print a, count
a= a
count= 1
}

Did you get the algorithm ?

no

ok, the first thing i need to do is to store the User input in an array??
i dont know how to do this
i cant find a perfect loop for this

I think you need to do a bit of reading on C
Do you have any books, tutorials on C ?
If not there are some links available in this forum

i try this but its not working

int main() {
    char c;                
    int count;
    char arr[50];
    c = getchar();         
    count = 0;
    while (c != EOF) {     
        arr[count] = c;   
        ++count;          
    }
    system("PAUSE");
    return (0);
}

You could pass the input string as a command line argument

int main(int argc,char* argv[])
{
   printf("%s\n",argv[1]);
}

and then when you execute the program, you execute it like this ./test input string

I seriously think you need to read C from a book.

ok i'll try
just started to learn C and dont understand anythin i books :(

i learn while doin small programs :(

This is not a simple program.
First step would be to i/p and then o/p a string.
Then you should see if you can count the length of the string

My favorite book for C is "Let Us C" by Yashwant kanitkar
It teach C from the very basic and makes no assumption about the programming capability of the reader

This is not a simple program.
First step would be to i/p and then o/p a string.
Then you should see if you can count the length of the string

My favorite book for C is "Let Us C" by Yashwant kanitkar
It teach C from the very basic and makes no assumption about the programming capability of the reader

hello,
i got it, but its only for example AAAAABBBBBBBCCBBBAABBCCCCCS

#include <stdio.h>
#include <stdlib.h>

int main(){
char ch;
char arr[]="AAAAABBBBBBBCCBBBAABBCCCCCS";
int i = 0;
int count = 0;
while(arr[i] != '\0'){
if(arr[i] == arr[i+1]){
++count;
}else{
printf("%c%i", arr[i], count+1);
count = 0;
}
i++;

}
system("PAUSE");
}

how i'll do it to read any input from user?

thanks

Well to take input from the user there are many ways.
The easiest one is through command line arguments
The next one is through scanf() function. But this function is very dangerous, has lot of pitfalls. So you should avoid using it
Another way of taking user input is to use fgets. This is the safest way, but it is slightly more complicated

i use scanf everywhere, but idont know how to make the array store the user input
i mean i would to this like this:

char ch;
scanf("%s",&ch);
char arr[]=ch;

will this work

Just think for a moment what you are doing.

In line 2 you are asking the computer to store a string in a char. So if I give the input as ABCDEFGH it will be stored in a char
Then in line 3 you are telling the computer to store that char in an array .

Is this what you really want ?

This is a C++ snippet I made a long time ago, it's practically C and with some searching I'm sure you would have found it anyway. It's well commented so it should help you. [link]

This is a C++ snippet I made a long time ago, it's practically C and with some searching I'm sure you would have found it anyway. It's well commented so it should help you. [link]

Hello,
thanks for the link, but it count occurrences only for (the quick brown fox jumps over the lazy dog)

wat i need is to count occurrences from a user input phrase?

thanks again well commented one ;)

Just think for a moment what you are doing.

In line 2 you are asking the computer to store a string in a char. So if I give the input as ABCDEFGH it will be stored in a char
Then in line 3 you are telling the computer to store that char in an array .

Is this what you really want ?

i think i need something like this
all i need to do is to store user input in the array.

well, here you go...

//first think of the max length of your array and make it a constant;
const int MAX_SIZE = 256;
//then declare your array
char sentence[MAX_SIZE];
//now you need to ask the user to enter something
printf ("please enter the string \n");
//now comes the main part...storing the user input in your array...get ready...
fgets (sentence, MAX_SIZE, stdin);

that's it...your array will get filled with whatever the user inputs (even if that is lesser than 256 chars, no probs. The function will store the input and append it with a null character. For e.g if you enter "hello" your array will look like:

sentence[] = {'h', 'e', 'l', 'l', 'o', '\0'} the other spaces will simply not matter.


BTW, to use this array in the code snippet by William Hemsworth you need to modify the function call a bit like:

int index = 0;
while ( add_letter(sentence[index++], counters) );

hope it helps.


P.S: Sometimes its a good idea to initialize your array as char sentence[MAX_SIZE + 1] ( note the + 1). This way you guarantee the space for the null character. It all depends on the coder though. Like in this example I assumed (A very foolish assumption, though) that the sentence length won't go beyond 256 and there will be at least a single space for my beloved Mr. '\0'.

Well, I think what manutd4life is looking for is the run-length encoding algorithm, a simple algorithm for lossless data compression in which a sequence of the same character (Byte value) is stored as a single character (Byte value) following by its count, e.g. sequence aaaaaabbbc is RLE-coded by a6b3c1, just as manutd4life already explained (however, more usual is 6a3b1c coding).

There can be a vast number of code snippets found on the web, just searching for "run length encoding c++".

-- tesu

@tesu:

O yes...i got carried away, actually the title of this thread is a bit misleading...


@manutd4life:

in that case the code snippet is not exactly what you want...
But i guess it'll help you come up with this algo, actually it just needs a li'l tweaking...
and my previous post showed you how to take string inputs and store them in char arrays...

good luck.

i did it like this:

#include <stdio.h>
#include <stdlib.h>

int main(){
    char arr[999]; //="BBBBCCCCTTTTTTNNNNNNN";
    int i = 0;
    int count = 0;
    printf("Enter a series of charater");
    scanf("%s",&arr);
    while(arr[i] != '\0')
    {
         if(arr[i] == arr[i+1])
         {
               ++count;
         }
         else
         {
              printf("%c%i", arr[i], count+1);
              count = 0;
          }
        i++;
     }
     printf("\n");
system("PAUSE");
}

Now am i need to save it in a file:
here's my full work:

#include <stdio.h>
#include <stdlib.h>
#define NULL 0

int main(){
    char arr[999]; //="BBBBCCCCTTTTTTNNNNNNN";
    int i = 0;
    int count = 0;
    FILE *point;
    printf("Enter a series of charater: ");
    scanf("%s",&arr);
    point = fopen("occurrence.txt", "w+");
    while(arr[i] != '\0')
    {
         if(arr[i] == arr[i+1])
         {
               ++count;
         }
         else
         {
             if (point == NULL)
               {
                  printf("\nError can't open file\n");
                  }
               else
               {
                  fprintf(point, "%c%i ", arr[i], count+1);
                  printf("%c%i ", arr[i], count+1);
                  count = 0;
                  fclose(point);
                  }
          }
        i++;
     }
     printf("\n");
system("PAUSE");
}

THE problem is that when writing in the text file, it write only the first one
for example
user input: AAAABBBBBBCCCCTTTTT
on screen display: A4 B6 C4 T5

thats gud

but in the text file there is only A4

any help

urgent please
thanks every1 for help

You are closing the file, the first time you enter the else statement block. So the next time you come to the else block you will not be able to write anything to the file

Instead close the file when you come out of the while loop

You are closing the file, the first time you enter the else statement block. So the next time you come to the else block you will not be able to write anything to the file

Instead close the file when you come out of the while loop

Thank you very much
its working
thank you

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.