C++ program to expand the given string

C++ program to expand the following string
aa2b4c3d
o/p should be :
aabbccccddd

Questions by manish521

Showing Answers 1 - 30 of 30 Answers

Sanjib

  • Mar 27th, 2014
 

Code
  1. #include<iostream.h>

  2. #include<conio.h>

  3. #include<string.h>

  4. void main()

  5. {

  6.         char *str;

  7.         int i,n=0,chint,len,j;

  8.         clrscr();

  9.         cout<<"

  10. Enter the string to expand:

  11. ";

  12.         cin>>str;

  13.         len=strlen(str);

  14.         cout<<"

  15. -------OUTPUT-------------

  16. ";

  17.         for(i=0;i<len;i++)

  18.         {

  19.                 if((str[i]>=a && str[i]<=z)||(str[i]>=A && str[i]<=Z))

  20.                 {

  21.                         do

  22.                         {

  23.                                 cout<<str[i];

  24.                                 n--;

  25.  

  26.                         }while(n>0);

  27.                         n=0;

  28.                 }

  29.                 else

  30.                 {

  31.                         chint=(int)str[i]-48;

  32.                         n=(n*10)+chint;

  33.                 }

  34.         }

  35.         getch();

  36.  

  37. }



  Was this answer useful?  Yes

pankaj

  • Jul 23rd, 2014
 

input : p3s
output: psss

  Was this answer useful?  Yes

Aparna

  • Oct 19th, 2014
 

Code:

Code
  1. #include <iostream>

  2. #include <string.h>

  3. using namespace std;

  4.  

  5. int main()

  6. {

  7.    char *ipstr;

  8.    int len,i,cnt=1;

  9.    cout << "Enter String" << endl;

  10.    cin >> ipstr;

  11.    cout << "Output" << endl;

  12.    len = strlen(ipstr);

  13.    

  14.    for (i=0;i<len;i++)

  15.    {

  16.        if((ipstr[i]>=a && ipstr[i]<=z)||(ipstr[i]>=A && ipstr[i]<=Z))

  17.        {

  18.            while(cnt>0)

  19.            {

  20.               cout << ipstr[i];

  21.               cnt--;

  22.            }

  23.            cnt = 1;

  24.        }

  25.        else

  26.        {

  27.            cnt = (int)ipstr[i] - 48;

  28.        }

  29.    }

  30.    return 0;

  31. }

  32.  

  Was this answer useful?  Yes

Ashish

  • Oct 31st, 2014
 

Code
  1. #include <iostream>

  2. #include <string>

  3. #include <stdlib.h>

  4.  

  5. using namespace std;

  6.  

  7. int main()

  8. {

  9.    cout << "Hello World" << endl;

  10.    string str="aa22b4c3d";

  11.    int count = 0;

  12.    int k=1;

  13.    

  14.    for( int i=0; i<str.length(); ++i){

  15.        //cout << str[i] << endl;

  16.        

  17.        if( str[i]>=a && str[i]<=z ){

  18.            for(int j=0; j<=count; ++j){

  19.                cout << str[i];

  20.            }

  21.            count = 0;

  22.            k=1;

  23.        }

  24.        else{

  25.            count = (str[i]-48)*k+count-1;

  26.            k=k*10;

  27.        }

  28.    }

  29.    return 0;

  30. }

  31.  

  32. Hello World

  33. aabbbbbbbbbbbbbbbbbbbbbccccddd

  34.  

  35.  



  Was this answer useful?  Yes

siva

  • Nov 6th, 2014
 

Below logic will work correctly. I think the first answe wont work if we have more than 2 digits like 222. But below one is generalized code and work for all sequence.

Code
  1.  

  2. #include <iostream>

  3. #include <string>

  4. #include <stdlib.h>

  5.  

  6. using namespace std;

  7.  

  8. int main()

  9. {

  10.    cout << "Hello World" << endl;

  11.    string str="aa22b4c3d";

  12.    int count = 0;

  13.    int k=1;

  14.  

  15.    for( int i=0; i<str.length(); ++i){

  16.        if( str[i]>=a && str[i]<=z ){

  17.            for(int j=0; j<count; j++){

  18.                cout << str[i];

  19.            }

  20.            count = 0;

  21.            k=1;

  22.        }

  23.        else{

  24.            count = (str[i]-48)*k;

  25.            k=(k*10)+1;

  26.        }

  27.    }

  28.    return 0;

  29. }

  Was this answer useful?  Yes

Matvey

  • Mar 16th, 2015
 

Code
  1. #include <iostream>

  2. #include <string>

  3.  

  4. using namespace std;

  5.  

  6. int main(int argc, char** argv) {

  7.     if (argc < 2) {

  8.         cout << "You have to enter a word" << endl;

  9.         return 1;

  10.     }

  11.  

  12.     string input(argv[1]);

  13.  

  14.     int counter = 0;

  15.     for (size_t i = 0; i < input.length(); ++i) {

  16.         if (input[i] > 0 && input[i] <= 9) {

  17.             counter = counter*10 + input[i] - 0;

  18.             continue;

  19.         }

  20.         if (counter) {

  21.             while(counter) {

  22.                 cout << input[i];

  23.                 counter--;

  24.             }

  25.         } else {

  26.             cout << input[i];

  27.         }

  28.     }

  29.     cout << endl;

  30.     return 0;

  31. }

  32.  

  Was this answer useful?  Yes

caps

  • Sep 24th, 2015
 

This code should work for both multiple digits and multiple characters.

Code
  1. #include <string>

  2.  

  3. auto expand(const std::string& in) -> std::string

  4. {

  5.     using namespace std::literals::string_literals;

  6.    

  7.     if (in.empty())

  8.         return ""s;

  9.        

  10.     const auto nums{"1234567890"s};

  11.     using string = std::string;

  12.     using sz = std::string::size_type;

  13.    

  14.     const auto n_beg(sz{in.find_first_of(nums)});

  15.     if (n_beg > 0)

  16.         return in.substr(0, n_beg) + expand(in.substr(n_beg));

  17.     const auto n_end(sz{n_beg + in.substr(n_beg).find_first_not_of(nums)});

  18.     const auto n(sz{static_cast<sz>(std::stoi(in.substr(n_beg, n_end - n_beg)))});

  19.    

  20.     auto multiply{[](auto count, const string& str) -> string

  21.     {

  22.         auto ret{string{str}};

  23.         while(count)

  24.         {

  25.             if (--count)

  26.                 ret += str;

  27.         }

  28.         return ret;

  29.     }};

  30.     const auto sub_end(sz{in.substr(n_end).find_first_of(nums)});

  31.     const auto next_end(sz{sub_end != std::string::npos ? n_end + sub_end : sub_end});

  32.     if (sub_end != std::string::npos)

  33.         return multiply(n, in.substr(n_end, next_end - n_end)) + expand(in.substr(next_end));

  34.    

  35.     return multiply(n, in.substr(n_end, next_end - n_end));

  36. }

  Was this answer useful?  Yes

Narendra

  • Oct 1st, 2015
 

Code
  1. // Making the code as simple as I can.

  2. #include <iostream>

  3. #include <string>

  4.  

  5. using namespace std;

  6.  

  7. int main(void)

  8. {

  9.     int repeat;

  10.     string in;

  11.    

  12.     cout << "Provide the input

  13. ";

  14.     cin >> in;

  15.  

  16.     for (int i=0; i < in.length(); i++) {

  17.         if (in[i] > 48 && in[i] < 59) { // this is to check >0 && <10

  18.             repeat = repeat*10 + (in[i] - 48);

  19.             continue;

  20.         }

  21.        

  22.         if (repeat == 0) repeat = 1;    // for single characters

  23.  

  24.         for (repeat; repeat > 0; repeat--)

  25.             cout << in[i];

  26.     }

  27.     return 0;

  28. }

  29.  

  Was this answer useful?  Yes

Jin Hyuk Cho

  • Feb 3rd, 2016
 

A working example

Code
  1. #include <iostream>

  2. #include <string>

  3. #include <locale>

  4. #include <sstream>

  5.  

  6. using namespace std;

  7. string expand (string compressed) {

  8.     locale loc;

  9.     stringstream ss;

  10.    

  11.     // given aa2b4c

  12.     int howMany = 1;

  13.     for (string::iterator it=compressed.begin(); it != compressed.end(); it++) {

  14.        

  15.         if (isdigit(*it,loc)) {

  16.             howMany = (int)(*it - 0);

  17.         } else {

  18.             for (int i = 0; i < howMany; ++i) {

  19.                 ss << *it;

  20.             }

  21.             howMany = 1;

  22.         }

  23.     }

  24.     // result aabbcccc

  25.     return ss.str();

  26. }

  27.  

  28. int main () {

  29.     cout << expand("aa2b4c3d") << endl;

  30.     return 0;

  31. }

  32.  

  Was this answer useful?  Yes

Sai Vamshi

  • Nov 25th, 2018
 

Code
  1. #include <iostream>

  2. #include<string.h>

  3. using namespace std;

  4.  

  5. int main()

  6. {

  7.     char *str;

  8.     cin>>str;

  9.     int len = strlen(str);

  10.     for(int i=0;i<len;i+=2){

  11.             for(int j=0;j<(str[i+1]-0);j++){

  12.                 cout<<str[i];

  13.             }

  14.     }

  15.     return 0;

  16. }

  17.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions