Hi there, I'm trying to write code for an assignment that asks me to read a string and convert all uppercase letters in it to lowercase without using tolower(), since we haven't covered it yet. This is what I have so far:

#include <iostream>
using namespace std;

int main (void)
{
	const int max = 50 ;
	char str[max] ;
	cin >> str ;

	for(int i = 0 ; i <= max ; i++)
	{
		if(str[i] >= 'A' && str[i] <= 'Z')
			str[i] += 32 ;

	}

	cout << str ;

return 0 ;
}

But every time I input a string that has more than one word, it only prints the first word in lower case. (For example, I put in "One tWo thRee" and only got back "one" so I know the first word got converted.) When I'm adding 32 up in the code I'm trying to use the ASCII code, but I'm wondering if it's an issue with white spaces? Help would be appreciated!

When you use cin >> to save a string, it only gets the first word up to the whitespace. You need to wrap everything between lines 8 and 17 in another loop that performs the collection from standard input until you run out of characters.

Use the cin.getline() function instead of cin. Your code should look like this.

cin.getline(str, max)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.