I just want to write an code to change the input to another text with another structure. for example a text is given. and I should convert them intoanother text like I say below.
this is the input :
typedef enum
{
RED,
GREEN,
BLUE
} Color;
and this should be the output :
enum class Color
{
RED = 0,
GREEN = 1,
BLUE = 2
};
#include <iostream>
#include<string>
#include <vector>
using namespace std;
int main()
{
string example = "typedef enum {RED,GREEN,BLUE} Color;";
string var_name;
for(int i=0; i <example.size(); i )
{
if(example[i] == ';') break;
var_name = example[i];
}
//cout << var_name << endl;
int i = var_name.length() - 1; // last character
while (i != 0 && !isspace(var_name[i]))
{
--i;
}
string lastword = var_name.substr(i 1); // 1 to skip leading space
cout << lastword << endl;
///////////////////////////////
return 0;
}
I could only get the color. can anybody help me to complete this:)? the enum of c is just a text and I have to convert it into an another text which look like an anum in c . its just working with strings and charecters.
CodePudding user response:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string temp;
vector <string> lines;
while(true){
getline(cin,temp);
lines.push_back(temp);
if(temp.back()==';')
break;
}
cout<<"enum class "<<lines.back().substr(2,lines.back().size()-3)<<endl;
cout<<lines[1]<<endl;
for(int i=2 ; i<lines.size()-1 ; i ){
if (i!=lines.size()-2){
cout<<lines[i].substr(0,lines[i].size()-1)<<" = "<< i-2<< ","<<endl;
}
else
cout<<lines[i]<<" = "<< i-2<<endl;
}
cout <<"};";
}