Creating a function to lowercase a string.

I have to implement: string tolower(string str) , which lowercases all alphabetic characters in the string.

This code works by itself:

1
2
3
4
5
6
7
8
9
  string str = "HELLO";
  int i = 0;
	char c;
	while (str[i]) {
		c = str[i];
		str[i] = tolower(c);
		cout << str[i];
		i++;
	}


However, it won't work when I implement it with the function definition:

1
2
3
4
5
6
7
8
9
10
11
string tolower(string str) {

  int i = 0;
	char c;
	while (str[i]) {
		c = str[i];
		str[i] = tolower(c);
		cout << str[i];
		i++;
	}
}

Then call using: cout << tolower("HELLO");

Thank you for the help.
Last edited on
You need to return something from your function as it is declared as returning value. Additionally you do not need to output anything in your function at all.
Thanks for the advice. I ended up with:

1
2
3
4
5
6
7
8
9
10
11
string tolower(string str) {

  int i = 0;
	char c;
	while (str[i]) {
		c = str[i];
		str[i] = tolower(c);
		i++;
	}
return str;
}


But it still isn't working correctly, and I'm not really sure what I should do.

I get this compile error: none of the 2 overloads could convert all the argument types
Last edited on
It would be more helpful if you posted the full error message.
Here it is:

error C2665: 'tolower' : none of the 2 overloads could convert all the argument types
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\ctype.h(146): could be 'int tolower(int)'
1> while trying to match the argument list '(const char [6])'
Make sure your tolower function has been declared before you try to call it.
I forgot to include the parameters in the prototype -_-
Thank you for the help.
Topic archived. No new replies allowed.