String Conversion Algorithm

Problem Statements:

You are given a string which consist of characters and numbers of unknown length. It is in the following form: "nXnXnX....", where n equals a number and X a letter. (The 1st nX doesn't equal to the 2nd nX, for example nXnX could equal to 2A4B). Then, you have to output the string which is produced when you output n times the X character.

Example Input:
3A3F4R6A

Example Output:
AAAFFFRRRRAAAAAA


I came up with the following algorithm, but it was an epic fail, of course, because when a number is being input to a string it converts to a character, so if you try to make it an integer again you just get its ASCII value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>
using namespace std;


int main(){
string word;
cin>>word;

for (int i=0; i<word.size(); i=i+2){
    for (int j=0; j<word[i]; j++){
        cout<<word[i+1];
    }
}
}
Last edited on
so if you try to make it an integer again you just get its ASCII value.

You need to convert your character to an int like this.
1
2
char c1 = '5';
int num = c1 - '0'; // num = 5 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;


int main()
{
  string word;
  cin>>word;

  for (int i=0; i<word.size()-1; i=i+2)
  {
    char c1 = word[i];
    char c2 = word[i+1];
    int num = c1 - '0';
    for( int i = 0; i < num; i++)
      cout << c2;
  }
  system("pause");
}
Substrct that character number with '0' or 48, for an example '2'-'0' and you'll get 2 in int since '2' is equal to 50 in ascii
Oh now I get it :P. It's like when substracting 65 from S to find its place in the alphabet.
Just found out that the c1 - '0' solution is not correct. If you have like 20A it will take 2 and 0 as different numbers.
A stringstream is an easy way to parse the input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string line("3A3F20A4R6A");
    istringstream ss (line);

    int num;
    char ch;
    
    while (ss >> num >> ch)
    {
        cout << "number: " << num 
             << "  character: " << ch << '\n';
    }
    
    return 0;
}
number: 3  character: A
number: 3  character: F
number: 20  character: A
number: 4  character: R
number: 6  character: A
Even though I know it's correct since I ran it in my offline compiler, hacerrank's autocompile engine says the outputs are wrong. It's a problem with the online compiler, so is there any way to do this with another way?

https://www.hackerrank.com/contests/exercises6/challenges/exe4fe6/copy-from/4360099
I've no idea what the linked page was supposed to be - I get a big 404 not found.

If you show your latest code then there might be something that could be adjusted.

As for "is there any way to do this with another way?" - well of course. There are always thousands of ways to tackle a problem. I merely suggested one which came readily to mind.
Last edited on
Topic archived. No new replies allowed.