is there a single function in c++ that converts strings from lowercase to uppercase and vice versa? I need to compare two strings case insensitive and I can only convert one of the strings and the other needs to be intact.
Asked
Active
Viewed 474 times
0
-
do you mean `std::string` or a `char *` string? – SingerOfTheFall Feb 11 '13 at 08:13
-
10 characters is not "a lot of work to convert". – Rapptz Feb 11 '13 at 08:13
-
@Rapptz He unrolls the loop. – Mark Garcia Feb 11 '13 at 08:14
-
@SingerOfTheFall I mean std:string – Feb 11 '13 at 08:15
-
@Rapptz I meant there are 10 slots in my custom structured array and each one have a string slot. But I need to compare the user inputted strings with the strings without going through a loop. – Feb 11 '13 at 08:16
-
@AlokSave I already looked at it, and it is not very helpful. – Feb 11 '13 at 08:18
-
@MokammelHossainSanju then none of the answers here are either. – Rapptz Feb 11 '13 at 08:18
-
It answers the question that is conveyed through your post. Whether you want to ask what is really being conveyed through the question is for you to ponder upon and improve on. – Alok Save Feb 11 '13 at 08:20
-
It sounds like you need to do a case insensitive comparison rather than converting a string and then comparing. – Peter Wood Feb 11 '13 at 08:29
-
1@PeterWood yes, you are right, a case insensitive comparison. – Feb 11 '13 at 08:31
-
Possible duplicate: [Case insensitive string comparison in C++](http://stackoverflow.com/q/11635/1084416) – Peter Wood Feb 11 '13 at 08:31
-
1As Peter's comment implies, uppercasing and then comparing is not in general the same as doing a case insensitive comparison. It probably suffices for your use case since this appears to be some school assignment, but I think it's worth pointing out. – R. Martinho Fernandes Feb 11 '13 at 08:31
-
@PeterWood I saw this but I am worried if my teacher doesnt have the boost library, the code wont work in his compiler. – Feb 11 '13 at 08:35
-
@R.MartinhoFernandes the thing I am worried about is if my teacher doesnt have the boost libraries the code wont work, and I cant talk to him in person either as this is an online class :( – Feb 11 '13 at 08:36
-
The first answer uses boost, the other answers don't. – Peter Wood Feb 11 '13 at 08:36
-
See also [Herb Sutter](http://www.gotw.ca/gotw/029.htm) – Peter Wood Feb 11 '13 at 08:42
-
@PeterWood I am trying if(strcasecmp(books[i].bookTitle,titleFind)==0) but doesnt work – Feb 11 '13 at 08:45
-
@PeterWood I tried
too,but it says could not convert – Feb 11 '13 at 08:49 -
Read the whole article. The asserts are for example to show what the solution must be able to produce. It's not what you need to do. It is in the header `
` if you really want it. – Peter Wood Feb 11 '13 at 08:52
3 Answers
4
You can use std::toupper
or std::tolower
, in combination with std::for_each
std::transform
.
#include <cctype>
#include <algorithm>
#include <string>
#include <iostream>
int main()
{
std::string s = "Hello, World!";
std::transform(s.begin(), s.end(), s.begin(), [](char c) {return std::toupper(c);});
std::cout << s << "\n";
}
Edit
I need to compare two strings case insensitive and I can only convert one of the strings and the other needs to be intact.
You can define a function that performs a case-insensitive comparison of two characters, then use it with std::equal
:
bool case_insensitive_comp(char lhs, char rhs)
{
return std::toupper(lhs) == std::toupper(rhs);
}
int main()
{
std::string s1 = ....;
std::string s2 = ....;
bool match = std::equal(s1.begin(), s1.end(), s2.begin(), case_insensitive_comp);
}
You might have to check that the length of the strings is the same before the call to std::equal
.

juanchopanza
- 223,364
- 34
- 402
- 480
1
Could use std::transform with std::toupper or std::tolower
std::string s("hello, world!");
std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper);

billz
- 44,644
- 9
- 83
- 100
-
-
-
1@MokammelHossainSanju: If you are asking that question, it is pretty much clear that you did not read the marked duplicate and if you didn't how can you say it is not helpful? – Alok Save Feb 11 '13 at 08:22
-
@billz it is actually a handful of strings which I have to compare with the string user inputs. – Feb 11 '13 at 08:23
-
@AlokSave I can not use a function that the teacher did not use in class, it is for a project. – Feb 11 '13 at 08:24
-
-
2@MokammelHossainSanju if you have such constraints you should specify it clearly in the question. Otherwise you are unlikely to get answers that are useful to your very specific domain. – juanchopanza Feb 11 '13 at 08:27
-
@billz I can only convert one of the strings to either upper or lower completely and compare it with the other string that is inside a 10 character long structured array. – Feb 11 '13 at 08:30
-
@MokammelHossainSanju update your question with `EDIT:` section, show your real question – billz Feb 11 '13 at 08:31
-
0
You can use:
std::tolower
click andstd::toupper
click withstd::transform
click- Boost, as in this question
- Simply iterate through the string, as you suggested in the question. To convert every character in the string, you will have to iterate through it, anway.

Community
- 1
- 1

SingerOfTheFall
- 29,228
- 8
- 68
- 105