Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Program to represent or compress ‘2ABCDE' to string ‘AABCDE'’

Posted By: Easy Tutor     Category: C++ Programming     Views: 3243

Each line in the input file will be of the format ‘nA...’ where n, the duplication factor, is an integer between 2 and 99 and A is an uppercase alphabetic character. A string may contain single characters not prefixed with a duplication factor. If this were not the case, for instance, the string ‘AABCDE’ would be compressed to ‘2A1B1C1D1E’. To avoid this, single characters will not be prefixed with a duplication factor. The string ‘AABCDE’ would be compressed to ‘2ABCDE’. The maximum length of each line in input file is 80 characters.

The uncompressed string, 40 characters per line (it may be necessary to break an uncompressed string over multiple lines).

Code for Program to represent or compress ‘2ABCDE' to string ‘AABCDE'’ in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <ctype.h>
 # include <conio.h>


 int main( )
    {
       clrscr( );

       fstream File("CP-21.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80);

         if(strcmp(Data,NULL)==0)
        break;

         char Duplication_factor[10]={NULL};

         int index=0;
         int length=strlen(Data);

         do
        {
           strset(Duplication_factor,NULL);

           while(isdigit(Data[index]))
              {
             char Temp[5]={NULL};

             Temp[0]=Data[index];

             strcat(Duplication_factor,Temp);

             index++;
              }

           int duplication_factor=0;

           duplication_factor=atoi(Duplication_factor);

           if(duplication_factor==0)
              cout<<Data[index];

           else
              {
             for(int count=0;count<duplication_factor;count++)
                cout<<Data[index];
              }

           index++;
        }
         while(index<length);

         cout<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to represent or compress ‘2ABCDE' to string ‘AABCDE'’ is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Ian Serben from United States Comment on: Jul 03
How about the other way around? If I input say, 2A3Bc, I want to get the output AABBBc. Also, how about if I don't want to get the innput from an external .txt file?

View All Comments