string compare?

Sep 3, 2008 at 5:04am
I have a file with names that I want my program to sort for a certain name. The names are sent into the file as strings. Is there a way to compare strings while keeping in mind the person searching may not use the same case settings(upper case and lower case) as the data file?
Sep 3, 2008 at 5:11am
Just convert both strings to lower or upper case and then do the comparison.
Sep 4, 2008 at 2:18am
I am fairly new to C++. The only way I know to convert case is toupper. I get the message that " 'toupper' : cannot convert parameter 1 from 'std::string' to 'int'"
the line was typed:

cout<<toupper(fFirst);

is there another way? or what am I doing wrong?
Sep 4, 2008 at 2:42am
toupper() only converts 1 character value to upper case.
Honestly, I don't really know the proper way to change the case of an std::string, nut here's something that would work:
1
2
3
std::string txt;
for (char *a=(char*)txt.c_str();*a;a++)
	*a=toupper(*a);

I know, it's not the best way of doing it. Someone is bound sooner or later to post a more STL-oriented solution.
Last edited on Sep 4, 2008 at 2:43am
Sep 4, 2008 at 2:42am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

#include <iostream>
using namespace std;

int main()
  {
  string users_input;
  cout << "Please enter something with upper- and lowercase letters\n> ";
  getline( cin, users_input );

  // non-destructive copy
  string up;
  transform(
    users_input.begin(),
    users_input.end(),
    back_inserter( up ),
    ptr_fun <int, int> ( toupper )
    );

  cout << "Original  = " << users_input << endl;
  cout << "UPPERCASE = " << up << endl;

  // destructive transform
  transform(
    users_input.begin(),
    users_input.end(),
    users_input.begin(),
    ptr_fun <int, int> ( tolower )
    );
  cout << "lowercase = " << users_input << endl;

  return 0;
  }

Hope this helps.

[edit] Argh. Too slow again... :-\
Last edited on Sep 4, 2008 at 2:43am
Sep 4, 2008 at 2:44am
Yes, just like that^.
Topic archived. No new replies allowed.