|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
C++ / Case insensitivity
there's no method in <string> to convert text to upper/lower case but i need to read some input from a file and search for a word in this text. Cases can be mixed - is there an easy way to do this or do i have to write a struct with new traits?
thanks |
|
#2
|
||||
|
||||
|
Re: C++ / Case insensitivity
Quote:
Use the transform function to convert a string to lowercase or uppercase: Code:
#include <string>
#include <algorithm>
#include <cctype>
....
transform(sString.begin(), sString.end(), // Start, end to transform on
sString.begin(), // Destination start
tolower // Operation to perform on string
);
|
|
#3
|
|||
|
|||
|
thankyou
![]() |
|
#4
|
||||
|
||||
|
Edited the code above so that you can figure out what the parameters mean.
|
|
#5
|
||||
|
||||
|
Just noticed you want to do a case-insensitive find. For that, you might as well have code like this:
Code:
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
bool nocase_compare (char c1, char c2)
{
return toupper(c1) == toupper(c2);
}
....
string::iterator pos;
pos = search (s1.begin(),s1.end(), // haystack
s2.begin(),s2.end(), // needle
nocase_compare); // comparison criterion
if (pos == s1.end()) {
// Not found
} else {
// String is found
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > C++ / Case insensitivity |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|