case insensitive find

I need to parse an input file that contains functions and comment lines.
But the functions are case insensitive so I need to figure out a way to find the functions being lowercase uppercase or a mix.
I thought I could use toupper but it only accepts char.
the 2 functions(in bold) in file are size=x,y and searchFor=word Commented lines can be blank or start with '#'.
Once the size is known we can parse a puzzle, which will be a char[][] type based on the size of the array. (Although this may be also invalid input).

I'm starting to think perhaps string class is not the best way to go about this, perhaps I should write a pile of functions to parse char* for example this could be a valid input file:
1
2
3
4
5
6
7
8
9
siZe=6,6 # this is a comment
SeArChFoR=bum # because we can
bumubx #
skwium
nvbsme # I like pie
puuuuw
mamxbl

# could be that the whole puzzle is on one line?  



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
void processFile(string infile)
{
   string line,word;
   size_t found;
   string size="SIZE=";
   string search="SEARCHFOR=";

   ifstream ifs(infile.c_str());
   if (! ifs.is_open())
   {
      ...
   }
   // else process as normal

   int line_count=0;
   while (! ifs.eof())
   {
      getline(ifs, line);
      cout << ++line_count << "\n";

      if (line == "" || line[0] == '#')
      {
         ; // line is comment and will be ignored.
      }
      else if (line[0] == 's' || line[0] == 'S')
      {
         //== function line or part of puzzle ==//
      
         found= line.find(size); // needs to be case insensitive
         if (found != std::npos)
         {
            // validate row and cols
            bool gotSize=false;
            gotSize = parseSize(line,0,0);
         }
         found = line.find(search);
         if (found != std::npos)
         {
            // figure out what to do here... (substring to space perhaps)
         }
      }
      else
      {
         //== Puzzle line ==//
         /*
         need to check if we have valid size value.
         check substring to ' ' and see if size == row
         */
      }
   }
}

bool parseSize(string line, int row, int col)
{
   int size_pos[3] = { 0 };
   string strRow, strCol;

   size_pos[0] = line.find('=');
   size_pos[1] = line.find(',');
   size_pos[2] = line.find(' ');

   strRow = line.substr(size_pos[0],size_pos[1]);
   strCol = line.substr(size_pos[1],size_pos[2]);

   // validate the above, return false if invalid.
   // parse the strings to integers row, col using stringstream

   return true;
}
To make a string upper case you can use toupper() in conjuction with transform():

1
2
3
4
5
6
7
8
9
10
#include <cctype> // for toupper()
#include <algorithm> // for transform()
#include <string>

using namespace std;

{
    string x = "aBcD";
    transform(x.begin(), x.end(), x.begin(), toupper);
}
Last edited on
1
2
3
4
void toUpper(string& str) {
    for(int x=0; x<str.length(); x++)
        str[x]=toupper(str[x]);
}
mmm thanks guys.

I really hope std::string class gets extended in "C++0x" to include functions like ignorecase(), toupper() etc. stoopid workarounds....
Last edited on
Topic archived. No new replies allowed.