Toy C++ program for custom string conversion

I am working on lexer part of compilers right now, the task I was doing is to putting up keyword definitions in cool.flex file. Keywords were given in another file in this format:

//for example keyword is class
#define CLASS 234

and I am supposed to be writing it in cool.flex like this:

//keywords are case-insensitive
[cC] + [lL] + [aA] + [sS] + [sS]        {return (CLASS);}

Instead of doing all this by hand I wrote this C++ program and it worked fine.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
 string str, str1;
 fstream myfile("test.file");
 ofstream file("temp.file");
 while(getline(myfile, str))
 {
 str1="";
 for(int i=0;i<str.length();i++)
 if(isupper(str[i]) && str[i+1]!=' ')
 {
 char ch=putchar(tolower(str[i]));
 file<<"["<<ch<<str[i]<<"]+";
 str1.push_back(str[i]);
 }
 else if(isupper(str[i]) && str[i+1]==' ')
 {
 char ch=putchar(tolower(str[i]));
 file<<"["<<ch<<str[i]<<"]";
 str1.push_back(str[i]);
 }
 file<<" { return("<<str1<<");} \n";
 }
 return 0;
}