Function to convert string to uppercase

May 10, 2012 at 8:09am
I need a function that takes a reference to a string object and that converts the contents of the string to uppercase.

1
2
3
4
5
string function(string s) {
	for (int i = 0; i < strlen(s); i++)
		s[i]=toupper(s[i]);
	return s;
}


When I compile, I get the error: Cannot convert 'string' to 'const char *'. Please help.
May 10, 2012 at 8:31am
Hi,
Try this version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <locale>

std::string Convert(std::string& str)
{
	std::locale settings;
	std::string converted;

	for(short i = 0; i < str.size(); ++i)
		converted += (std::toupper(str[i], settings));
	
	return converted;
}
int main()
{
	std::string parameter = "lowercase";
	std::cout << Convert(parameter);
	std::cin.ignore();
	return 0;
}
Last edited on May 10, 2012 at 8:34am
May 10, 2012 at 8:34am
But what's wrong with what I wrote and how can I fix it?
May 10, 2012 at 8:35am
The function strlen takes a char*, but you're feeding it a string. A char-pointer is not a string.

The C++ string object comes with a member function that gives its length.
May 10, 2012 at 8:37am
@thomas did you declare the header type. if not use <ctype.h> as the header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
}

May 10, 2012 at 8:37am
You can fix it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <ctype.h>

std::string Convert(std::string& str)
{
	std::string converted;

	for(short i = 0; i < str.size(); ++i)
		converted += toupper(str[i]);
	
	return converted;
}
Last edited on May 10, 2012 at 8:37am
May 10, 2012 at 8:48am
@codekiddy Thanks, that worked. But what is the difference between using 'string &' rather than 'string' as the function's argument? Both seem to work fine.
Last edited on May 10, 2012 at 8:48am
May 10, 2012 at 8:54am
when argument is string& then you are manipulating with original objet pased to the function.
This approach is called pass by reference.

When argument is string you are passing a copy of the parameter not the real object, thus if you change it inside the function your original copy outside the function will left untouched!

another reason is performance,
passing by reference is faster,
Why?
simply because you avoid copying the object.

I you look at this problem a little bit harder you may come to an interesting conclusion!

Your funciton does't have to return anything, instead it manipulates with original string!

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <ctype.h>

inline void Convert(std::string& str, std::string& converted)
{
	for(short i = 0; i < str.size(); ++i)
		converted += toupper(str[i]);
}

int main()
{
	std::string converted;
	std::string parameter = "lowercase";

	Convert(parameter, converted); // workng with real objects not copy of them

	std::cout << converted;
	std::cin.ignore();
	return 0;
}
Last edited on May 10, 2012 at 8:57am
May 10, 2012 at 9:21am
@codekiddy Thanks a ton!
Topic archived. No new replies allowed.